1

Title pretty much says it all. The score is being displayed as 0 (which is what I initialized it to). However, when updating the Score it's not propagating to the UI textBlock. Thought this would be pretty simple, but I'm always running into problems making the switch from Android :) Am I suppose to be running something on the UI thread??

I'm trying to bind to the "Score" property.

<TextBox x:Name="text_Score" Text="{Binding Score, Mode=OneWay}" HorizontalAlignment="Left" Margin="91,333,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Height="148" Width="155" FontSize="72"/>

Here is my holder class

   public class GameInfo
    {
        public int Score { get; set; }
        public int counter = 0;
    }

**Note: Make sure you don't forget to add {get; set;} or else nothing will show up.

and this is where I'm trying to set it

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    info.counter = (int)e.Parameter;

    text_Score.DataContext = info;
}

P.S. To reiterate, I'm going for OneWay. I only want to display the score and have it undated when the variable changes. I plan on disabling user input.

Here is the full working code example. The only thing that had to change was my holder class. Thanks Walt.

public class GameInfo : INotifyPropertyChanged
{
    private int score;
    public int Score {
        get { return score; }
        set
        {
            if (Score == value) return;
            score = value;
            NotifyPropertyChanged("Score");
        }
    }
    public int counter = 0;

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
Frank Sposaro
  • 8,511
  • 4
  • 43
  • 64

1 Answers1

2

In XAML binding your underlying class needs to inform the binding framework that the value has changed. I your example, you are setting the counter in the OnNavigatedTo event handler. But if you look at your GameInfo class, it's a simple data object.

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. So in your case, change the class as follows

public class GameInfo : INotifyPropertyChanged
{
    private int _score;
public int Score
{
  get
  {
    return this._score;
  }

  set
  {
    if (value != this._score)
  {
    this._score = value;
    NotifyPropertyChanged("Score");
  }
}

  }    
public int counter = 0; // if you use _score, then you don't need this variable.
    public event PropertyChangedEventHandler PropertyChanged;

     private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

See the MSDN article for more information INotifyPropertyChanged

Walt Ritscher
  • 6,977
  • 1
  • 28
  • 35
  • Well, I wasn't done with the code,it got posted anyway. I'll add some more, because the properties are calling NotifyPropertyChanged yet. – Walt Ritscher Oct 17 '12 at 22:39
  • You also need to call the NotifyPropertyChanged in the Score setter – Walt Ritscher Oct 17 '12 at 22:44
  • Thanks Walt. What your said makes sense. I was actually just searching around and was looking at an example of this. However, I'm not understanding what is the purpose of _score – Frank Sposaro Oct 17 '12 at 22:45
  • Ok. So I think you just left off the int. I guess the issue I'm having is what is reasoning behind have 2 variables for the same thing? – Frank Sposaro Oct 17 '12 at 22:48
  • In .NET, a common convention for properties is to have a backing variable with the same name as the Property. Some devs use _score, some use m_score, for the name of the backing variable. – Walt Ritscher Oct 17 '12 at 22:52
  • Thanks again. You have been really helpful in my growing pains into Windows App lol. For all those interested about the _score vs score check out http://stackoverflow.com/questions/12736237/using-a-backing-variable-for-getters-and-setters Apparently, it was needed in a prior version on .NET. However, I'm yet to be a fan and I'm in favor of the letting the compiler do the backing. Less code == less mistakes && easier understanding. – Frank Sposaro Oct 17 '12 at 22:57
  • 1
    Ah, but you can't access that auto generated backing field in your code. In this case, you are changing the value in the setter, in order to raise the NotifiyChanged event. So you have to have your own variable. – Walt Ritscher Oct 17 '12 at 23:08