我一直在努力写一个小的国际象棋游戏,我想到了我想在字段上显示威胁计数以帮助我调试。但它不起作用,即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也正确分配。我现在被困住了。