WPF 控件初始化时属性未更新
我是 WPF 新手,在从 MainWindow XAML 文件获取自定义用户控件的属性值时遇到问题。
在这里,我想获取值“8”作为行数和列数,但在我的 InitializeGrid() 方法中,从未设置这些属性。它们始终为“0”。我做错了什么?
任何参考文献也将不胜感激。
这是我的 MainWindow.xaml(相关部分):
<local:BoardView
BoardRows="8"
BoardColumns="8"
/>
这是我的 BoardView.xaml:
<UniformGrid
Name="uniformGrid"
Rows="{Binding BoardRows}"
Columns="{Binding BoardColumns}"
>
</UniformGrid>
</UserControl>
这是我的 BoardView.xaml.cs:
[Description("The number of rows for the board."),
Category("Common Properties")]
public int BoardRows
{
get { return (int)base.GetValue(BoardRowsProperty); }
set { base.SetValue(BoardRowsProperty, value); }
}
public static readonly DependencyProperty BoardRowsProperty =
DependencyProperty.Register("BoardRows", typeof(int), typeof(UniformGrid));
[Description("The number of columns for the board."),
Category("Common Properties")]
public int BoardColumns
{
get { return (int)base.GetValue(BoardColumnsProperty); }
set { base.SetValue(BoardColumnsProperty, value); }
}
public static readonly DependencyProperty BoardColumnsProperty =
DependencyProperty.Register("BoardColumns", typeof(int), typeof(UniformGrid));
public BoardView()
{
InitializeComponent();
DataContext = this;
InitializeGrid();
}
private void InitializeGrid()
{
int rows = BoardRows;
int cols = BoardColumns;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
uniformGrid.Children.Add( ... );
// ...
}
}
}
I'm new to WPF, and am having trouble getting the values for properties for a custom user control from the MainWindow XAML file.
Here, I want to get the value "8" as the number of rows and columns but in my InitializeGrid() method, the properties are never set. They are always "0". What am I doing wrong?
Any references will also be appreciated.
This is my MainWindow.xaml (the relevant portions):
<local:BoardView
BoardRows="8"
BoardColumns="8"
/>
This is my BoardView.xaml:
<UniformGrid
Name="uniformGrid"
Rows="{Binding BoardRows}"
Columns="{Binding BoardColumns}"
>
</UniformGrid>
</UserControl>
And this is my BoardView.xaml.cs:
[Description("The number of rows for the board."),
Category("Common Properties")]
public int BoardRows
{
get { return (int)base.GetValue(BoardRowsProperty); }
set { base.SetValue(BoardRowsProperty, value); }
}
public static readonly DependencyProperty BoardRowsProperty =
DependencyProperty.Register("BoardRows", typeof(int), typeof(UniformGrid));
[Description("The number of columns for the board."),
Category("Common Properties")]
public int BoardColumns
{
get { return (int)base.GetValue(BoardColumnsProperty); }
set { base.SetValue(BoardColumnsProperty, value); }
}
public static readonly DependencyProperty BoardColumnsProperty =
DependencyProperty.Register("BoardColumns", typeof(int), typeof(UniformGrid));
public BoardView()
{
InitializeComponent();
DataContext = this;
InitializeGrid();
}
private void InitializeGrid()
{
int rows = BoardRows;
int cols = BoardColumns;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
uniformGrid.Children.Add( ... );
// ...
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已设置此绑定:
问题是您的绑定不起作用,因为该绑定使用默认数据源,即
UserControl
的DataContext
。您可能还没有设置DataContext
,但这没关系,因为无论如何这都不是您想要的。您想要将
UniformGrid
中的Rows
数量绑定到BoardView.BoardRows
属性。由于UserControl
是前面的代码片段是一个BoardView
,您可以给BoardView
一个名称并使用ElementName
语法如下引用它:这表示:“将
UniformGrid.Row
绑定到名为boardView 的元素的
”,就是你的 想!BoardRows
属性You have this binding set up:
The problem is that your binding is not working because the binding uses the default data source which is the
DataContext
of theUserControl
. You probably haven't set theDataContext
but that's OK because that's not what you want anyway.You want to bind the number of
Rows
in theUniformGrid
to theBoardView.BoardRows
property. Since theUserControl
is the previous code snippet is aBoardView
, you can give theBoardView
a name and use theElementName
syntax to refer to it like this:This says: "Bind
UniformGrid.Row
to theBoardRows
property of the element namedboardView
", just what you want!