Question

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));
    }
}

In the view

 <StackPanel DataContext="{Binding Path=Game.Player1}">                    
     <TextBlock Text="{Binding TotalScores}"/>
 </StackPanel>

The DataContext of the View is set to GameBoardPageViewModel by Caliburn.

 public class GameBoardPageViewModel {
      public Game Game {
        get { return game; }
    }
 }

I see in the debugger that when AddMove is invoked, there is nobody listening to the PropertyChanged event (AddMove is invoked much more later than View and ViewModel constructing, so bindings are have to be established at that moment).

Update 1. If I call a notification of the Game property, then binding works. What is the cause of this behaviour?

Was it helpful?

Solution

If I call notification on the Game property, then the binding works

Since you do not raise PropertyChanged event for Game property it must be available at the time of binding otherwise binding mechanism won't be able to create link.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top