Question

Previously in Windows Phone Silverlight apps, we could implement INotifyPropertyChanged and raise it:

    public int Id
    {
        get { return id; }
        set
        {
            if (value != id)
            {
                id = value;
                NotifyPropertyChanged("Id");
            }
        }
    }

Currently, in in default hub template of Windows Runtime App, I see we use

public ObservableCollection<Item> Items{ get; set; }

It will only detect if an item of collection added or removed, But how about changing an existing item's property? I change a property of an item, but it doesn't take effect. what is wrong? thanks.

Update: I see we still can do that and works. But is it the recommended way to do in Windows Runtime apps?

Was it helpful?

Solution

Inside your Item class you want to implement INotifyPropertyChanged in that class. Anything you want to modify to the existing items' property will reflect to the UI by calling RaisePropertyChanged() event.

public class Item : INotifyPropertyChanged
{
  // Omitted implementation of INPC

  private string _name;
  public string Name { get { return name; } set { _name = value; RaisePropertyChanged(); } }

  public void Update()
  {
     Name = "NewValue"; // This will show up in the UI if it is bound to a property (DataBinding)
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top