2012-09-29 80 views
1

我有一個ListView其中:ListView控件不刷新其項目

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; 

我綁定的RowData屬性與我的專欄的DisplayMemberBinding特性,例如:

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

我處理ListViewItem.DoubleClick事件:

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

但是wh我將新字符串分配給我的數據ListView不刷新其項目(即使Box1具有新值 - 即在分配新字符串之前新雙擊顯示Box1 == "new string",我仍然可以看到舊值Box1)。
爲什麼?我怎麼解決這個問題?

+0

此信息可能對您有所幫助。 http://stackoverflow.com/questions/4680653/getting-a-wpf-listview-to-display-observablecollectiont-using-databinding –

回答

2

你忘了執行INotifyPropertyChangedinteface

後,您在您需要通知視圖進行自我更新你的數據類改變了一些屬性。

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

public event PropertyChangedEventHandler PropertyChanged; 

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