【发布时间】:2013-03-04 18:42:18
【问题描述】:
我有绑定到 ObservableCollection 的 WPF 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 拒绝显示它。
有什么想法吗?
【问题讨论】:
-
调试绑定问题的最佳方法通常是在 Visual Studio 的输出窗口中查找绑定错误
-
无需 INPC 您的可观察集合属性。
-
如果实例化一个新的 ObservableCollection,绑定在没有 INPC 的情况下还会更新吗?
-
好吧,ObservableCollection 已经包含 INPC,所以我不必手动实现它。
-
确实,收藏会通知。该问题与您的代码无关,因此很抱歉劫持。 @Will 对此进行了测试,如果您实例化一个新的
ObservableCollection而不在其上放置 INPC,则绑定不会更新。