【发布时间】:2016-09-08 22:28:48
【问题描述】:
我正在尝试将 int 变量从 MainWindow.xaml 传递给 UserControl。当我调试时 myGridSize 总是等于零。我将不胜感激。
MainWindow.xaml
x:Name="myWindow">
<Grid>
<my:SudokuGrid x:Name="mySudokuGrid" myGridSize="{Binding SudokuSize, ElementName=myWindow}">
</my:SudokuGrid>
</Grid>
主窗口代码
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>
用户控制代码
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());
}
【问题讨论】:
标签: c# wpf xaml binding user-controls