我一直在尝试编写一个小象棋游戏,我想到我想在字段上显示威胁计数以帮助我调试。但是它不起作用,即 Text 属性保持“”,我不知道为什么。
for (int i = 0; i < grid.ColumnDefinitions.Count; i++)
{
for (int j = 0; j < grid.RowDefinitions.Count; j++)
{
...
TextBlock textBlock = new TextBlock();
Binding binding = new Binding("WhiteThreats");
binding.Source = game.board[i][j];
textBlock.SetBinding(TextBox.TextProperty, binding);
...
}
}
其中 game.board[i][j] 是 GameField 类型,指定如下:
struct GameField : INotifyPropertyChanged
{
...
private int whiteThreats;
public event PropertyChangedEventHandler PropertyChanged;
public int WhiteThreats
{
get { return whiteThreats;}
set
{
whiteThreats = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("WhiteThreats"));
}
}
...
}
我已经检查了我想到的所有其他可能性。TextBox 显示正确,并且 binding.Source 也正确分配。我现在被困住了。