0
public class Game {
public Player Player1 {
get { return gameSettings.Player1; }
}
}
public class Player : INotifyPropertyChanged {
public int TotalScores {
get { return moves.Sum(move => move.ComposableWord.Length); }
}
public void AddMove(Move move) {
if (move == null)
throw new ArgumentNullException("move", "Move can not be null.");
moves.Add(move);
OnPropertyChanged("TotalScores");
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
在视图为什么绑定不起作用?
<StackPanel DataContext="{Binding Path=Game.Player1}">
<TextBlock Text="{Binding TotalScores}"/>
</StackPanel>
观的DataContext的由卡利设置为GameBoardPageViewModel。
public class GameBoardPageViewModel {
public Game Game {
get { return game; }
}
}
我在调试器,当AddMove调用看到,有没有人听PropertyChanged事件(AddMove得多晚援引比视图和视图模型构建,所以绑定拥有的时候要建立) 。
更新1. 如果我打电话给游戏属性的通知,然后绑定工程。这种行为的原因是什么?
您确定添加的Move的ComposableWord属性的长度大于0吗? – nemesv
是的。绝对。所有工作都正常。如果我在Game属性上调用通知,则绑定起作用。 – EngineerSpock
何时分配“游戏”?绑定时是否可用?既然你没有通知它必须在那个时候可用,否则它不会工作 – dkozl