2

I have a ListView where:

public class RowData
{
    public string Box1 { get; set; }
    public string Box2 { get; set; }
    public string Box3 { get; set; }
};

private ObservableCollection<RowData> Data = new ObservableCollection<RowData>();

...

MyListView.ItemsSource = Data;

I binded the properties of RowData with the DisplayMemberBinding property of my columns, for example:

MyGridViewColumn.DisplayMemberBinding = new Binding("Box1"));

I handle the ListViewItem.DoubleClick event:

private void ListViewItem_DoubleClick(object sender, MouseButtonEventArgs e)
{
    ListViewItem item = sender as ListViewItem;
    RowData data = item.DataContext as RowData;
    data.Box1 = "new string";
}

But when I assign the new string to my data the ListView does not refresh its items (I can still see the old value of Box1, even if Box1 has a new value - a new double click shows that Box1 == "new string" before assigning the new string).
Why? How can I solve this problem?

Nick
  • 10,309
  • 21
  • 97
  • 201
  • This post might help you. http://stackoverflow.com/questions/4680653/getting-a-wpf-listview-to-display-observablecollectiont-using-databinding – Furqan Safdar Sep 29 '12 at 16:04

1 Answers1

4

You forgot to implement INotifyPropertyChanged interface

After you updated any property in your data class you need to inform View to update itself.

public class RowData : INotifyPropertyChanged
{
    private string box1;
    public string Box1 
    {
        get { return box1; }
        set
        {
            if(box1 == value) return;
            box1= value;
            NotifyPropertyChanged("Box1 ");
        }
    }
 //repete the same to Box2 and Box3

 public event PropertyChangedEventHandler PropertyChanged;

 private void NotifyPropertyChanged(String propertyName)
 {
     if (PropertyChanged != null)
     {
         PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     }
 }
 }
Anton Sizikov
  • 9,105
  • 1
  • 28
  • 39