2013-03-04 125 views
2

我有WPF ListView绑定到ObservableCollection。更改项目时,WPF ListView不会更新DisplayMemberPath MVVM

这里是我的ListView:

<ListView 
       BorderBrush="#6797c8" 
       BorderThickness="2" 
       ItemsSource="{Binding Path=MainCategoriesCollection, UpdateSourceTrigger=PropertyChanged}" 
       DisplayMemberPath="Category" 
       SelectedValuePath="MainCatID" 
       SelectedItem="{Binding MainCategorySelectedItem}" 
       SelectedIndex="{Binding MainCategorySelectedIndex, Mode=TwoWay}" 
       FontSize="14"/> 

,这是我的ItemSource:

private ObservableCollection<DataModel.MainCategories> mainCategoriesCollection; 
public ObservableCollection<DataModel.MainCategories> MainCategoriesCollection 
{ 
    get 
    { 
     if (mainCategoriesCollection == null) 
     { 
      mainCategoriesCollection = new ObservableCollection<DataModel.MainCategories>(); 
     } 
     return mainCategoriesCollection; 
    } 
    set 
    { 
     mainCategoriesCollection = value; 
     RaisePropertyChanged("MainCategoriesCollection"); 
    } 
} 

我了有线问题。当我从MainCategoriesCollection添加项目或删除项目我的ListView得到更新没有任何问题,但是当我采取具体项目,并更改代表“DisplayMemberPath”的项目属性时,我看不到ListView中的更改。我调试了这个问题,看到MainCategoriesCollection中存在的变化,但是我的ListView拒绝显示它。

任何想法?

+2

调试绑定问题的最佳方法通常只是在Visual Studio的输出窗口中查找绑定错误 – ghord 2013-03-04 18:46:41

+0

无需INPC您的可观察集合属性。 – Will 2013-03-05 16:07:59

+0

如果您实例化一个新的ObservableCollection,绑定仍然会在没有INPC的情况下更新吗? – 2013-03-05 22:04:32

回答

4

确保DisplayMemberPath属性位于具有所有INotifyPropertyChanged内容的视图模型上,即属性observable?

+0

你说得对。谢谢 – Ofir 2013-03-04 19:01:19