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());
}