11

I'm trying to pass int variable from MainWindow.xaml to UserControl. When I'm debbuging myGridSize is always equal zero. I would appreciate any help.

MainWindow.xaml

x:Name="myWindow"> <Grid> <my:SudokuGrid x:Name="mySudokuGrid" myGridSize="{Binding SudokuSize, ElementName=myWindow}"> </my:SudokuGrid> </Grid> 

MainWindow code

 public static readonly DependencyProperty SudokuSizeProperty = DependencyProperty.Register("SudokuSize", typeof(int), typeof(MainWindow), new FrameworkPropertyMetadata(null)); private int SudokuSize { get { return (int)GetValue(SudokuSizeProperty); } set { SetValue(SudokuSizeProperty, value); } } public MainWindow() { SudokuSize = 9; InitializeComponent(); } 

UserControl.xaml

x:Name="gridCustom"> <UniformGrid Name="SudokuUniGrid" Style="{StaticResource CustomGridStyle}"> </UniformGrid> 

UserControl code

 public static readonly DependencyProperty myGridSizeProperty = DependencyProperty.Register("myGridSize", typeof(int), typeof(SudokuGrid), new FrameworkPropertyMetadata(null)); public int myGridSize { get { return (int)GetValue(myGridSizeProperty); } set { SetValue(myGridSizeProperty, value); } } UniformGrid BuildGrid() { //myGridSize = 9; UniformGrid theGrid = new UniformGrid(); for (int i = 0; i < myGridSize * myGridSize; ++i) { myButton button = new myButton(); button.cmLen = myGridSize; button.Width = 30; button.Height = 30; button.Tag = i; theGrid.Children.Add(button); } return theGrid; } public SudokuGrid() { InitializeComponent(); SudokuUniGrid.Children.Add(BuildGrid()); } 

1 Answer 1

20

A few issues.

Your dependency property needs to be registered to the user control type. Also you need to wait for the user control to be fully loaded before accessing.

MainWindow.xaml

<Window x:Class="WpfApplication5.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication5" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525" x:Name="myWindow"> <Grid> <local:SudokuUniGrid myGridSize="{Binding SudokuSize, ElementName=myWindow}"> </local:SudokuUniGrid> </Grid> </Window> 

MainWindow.xaml.cs

public partial class MainWindow : Window { public static readonly DependencyProperty SudokuSizeProperty = DependencyProperty.Register("SudokuSize", typeof(int), typeof(MainWindow), new FrameworkPropertyMetadata(null)); private int SudokuSize { get { return (int)GetValue(SudokuSizeProperty); } set { SetValue(SudokuSizeProperty, value); } } public MainWindow() { SudokuSize = 9; InitializeComponent(); } } 

SudokiGrid.xaml (user control)

<UserControl x:Class="WpfApplication5.UserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <UniformGrid x:Name="SudokuUniGrid"> </UniformGrid> </UserControl> 

SudokiGrid.xaml.cs (user control)

public class SudokuUniGrid : UserControl { public static readonly DependencyProperty myGridSizeProperty = DependencyProperty.Register("myGridSize", typeof(int), typeof(UserControl), new FrameworkPropertyMetadata(null)); public int myGridSize { get { return (int)GetValue(myGridSizeProperty); } set { SetValue(myGridSizeProperty, value); } } public SudokuUniGrid() { InitializeComponent(); Loaded += SudokuUniGrid_Loaded; } private void SudokuUniGrid_Loaded(object sender, RoutedEventArgs e) { Console.WriteLine(myGridSize); // build here } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Your solution is working. Now i understand what was a real problem. Thank you very much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.