tags:

views:

1234

answers:

2

I am building a simple roulette app. The player(UI) puts together a list of bets and submits them to the table object to be evaluated and paid out. I've got the code to work and the game process goes smoothly. The problem is that after a turn I can't get the player balance(textblock) or the betlist(listview) to update. Is there some sort of global window refresh command I am missing, or do I have to manually set each of these to update somehow?

+1  A: 

WPF can take care of updating these values for you automatically, but you have to let it know when things have changed. Typically, this is done by using DependencyProperties on your model objects, but it can also be done by implementing INotifyPropertyChanged. In either case, when you update a property's value, the PropertyChanged event gets called; WPF automatically subscribes to this event when it binds to a value, and will update the UI when a change occurs. Without this notification, WPF won't check to see if the values in your object have changed, and you won't see the change reflected on the screen.

Nicholas Armstrong
A: 

What about implementing INotifyPropertyChanged, and bind the balance and the betlist to the controls you are using?

Something like:

public class Player : INotifyPropertyChanged
    {
     private int _balance;

     #region Properties

     public int Balance
     {
      get { return this._balance; }
      set
      {
       if (this._balance != value)
       {
        this._balance = value;
        NotifyPropertyChanged("Balance");
       }
      }
     }

     public BindingList<Bet> BetList { get; set; }

     #endregion // Properties

     private void NotifyPropertyChanged(string propertyName)
     {
      if (this.PropertyChanged != null)
       this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     }

     #region INotifyPropertyChanged Members

     public event PropertyChangedEventHandler PropertyChanged;

     #endregion
    }

    public class Bet
    {
     // some code
    }

For the binding list you wouldn't need to implement anything since it implements an interface that notifies changes to whatever is bound to (IRaiseItemChangedEvents). But then again you could be using a different approach.

Carlo