[英]Why won't my ObservableCollection<MenuItem> update on RaisePropertyChanged?
In my WPF app, I have an ObservableCollection<MenuItem>
which is dynamically updated to contain various category items. 在我的WPF应用程序中,我有一个
ObservableCollection<MenuItem>
,它会动态更新以包含各种类别项。 When the screen loads, everything is fine but changing the collection while the screen is up, and calling RaisePropertyChanged
for the collection doesn't update the context menu. 当屏幕加载时,一切都很好,但在屏幕启动时更改集合,并且为集合调用
RaisePropertyChanged
不会更新上下文菜单。
It appears that possibly, context menu's are only loaded on control loading and can't be changed dynamically? 看起来可能,上下文菜单只在控件加载时加载,不能动态更改?
UPDATE: This code is dynamically creating a sub context menu based on items the user is adding or removing. 更新:此代码根据用户添加或删除的项目动态创建子上下文菜单。 My question here is why isn't the DocumentExplorerMenuOptions getter being called when RaisePropertyChanged is being called?
我的问题是为什么在调用RaisePropertyChanged时不调用DocumentExplorerMenuOptions getter? I see RaisePropertyChanged being called but the getter never gets asked for anything.
我看到RaisePropertyChanged被调用,但getter永远不会被要求任何东西。
public ObservableCollection<MenuItem> DocumentExplorerMenuOptions {
get {
return new ObservableCollection<MenuItem> {
new MenuItem(localizationProvider.Takeoff) {
Command = new DelegatingCommand(x => windowManager.ShowTakeoffWithGrid(context, selectedDocument, documentBagTasks))
},
new MenuItem(localizationProvider.TakeoffImage) {
Command = new DelegatingCommand(x => windowManager.ShowTakeoff(context, selectedDocument, documentBagTasks))
},
ClassificationMenuItem
};
}
}
MenuItem classificationMenuItem;
public MenuItem ClassificationMenuItem {
get { return classificationMenuItem; }
set { classificationMenuItem = value; }
}
void BuildClassificationsContextMenuItem(ICategoryBag categoryBag) {
ClassificationMenuItem = new MenuItem(localizationProvider.ClassifyAs);
if (categoryBag == null) return;
foreach (var category in (from category in categoryBag.Categories select category).OrderBy(x => x.Name)) {
AddCategoryMenuItem(category);
}
RaisePropertyChanged(DocumentExplorerMenuOptionsPropertyName);
}
void RemoveCategoryMenuItem(Category category) {
ClassificationMenuItem.Children.RemoveAll(x => x.Text == category.Name);
RaisePropertyChanged(DocumentExplorerMenuOptionsPropertyName);
}
void AddCategoryMenuItem(Category category) {
var categoryMenuItem = new MenuItem(category.Name);
ClassificationMenuItem.Children.Add(categoryMenuItem);
foreach(var value in category.Values) {
categoryMenuItem.Children.Add(new MenuItem(value.Name) { Command = new DelegatingCommand(x => classifier.Classify(new ClassificationArguments(category, value, new List<Document>(documentExplorer.SelectedDocuments).ToArray()))) });
}
RaisePropertyChanged(DocumentExplorerMenuOptionsPropertyName);
}
The ObservableCollection property is not setup correctly. ObservableCollection属性未正确设置。 You have added everything in to the getter of that property, that doesn't really work.
您已将所有内容添加到该属性的getter中,但实际上并不起作用。 Try creating a private member and instantiate that on the constructor itself and add those entries there.
尝试创建一个私有成员并在构造函数本身上实例化它并在那里添加这些条目。 In this case you are kind of re-instantiating the ObservableCollection for each of your View loading, because getter can get called many times.
在这种情况下,您可以为每个View加载重新实例化ObservableCollection,因为getter可以被多次调用。
This is strange, if I'm reading your code correctly. 如果我正确地阅读你的代码,这很奇怪。 Is there a reason that you can't maintain the collection and modify it (as opposed to re-creating it) so that you don't need to
RaisePropertyChanged
at all? 有没有理由不能维护集合并修改它(而不是重新创建它),这样你根本不需要
RaisePropertyChanged
? That's what ObservableCollection(of T)
does, after all. 毕竟,这就是
ObservableCollection(of T)
作用。
As I'm reading it, it might as well be an IEnumerable(of T)
. 当我读它时,它也可能是一个
IEnumerable(of T)
。
(Creating a new ObservableCollection(of T)
in a property getter is almost always a mistake. A ReadOnlyObservableCollection(of T)
makes a lot more sense in that context.) (在属性getter中创建一个新的
ObservableCollection(of T)
几乎总是一个错误。在该上下文中, ReadOnlyObservableCollection(of T)
更有意义。)
This issue was resolved on a similar query by me. 我在类似的查询中解决了这个问题。 Hope this resolves your issue.
希望这可以解决您的问题。
ContextMenu bound to ObservableCollection<MenuItem> does not refresh data 绑定到ObservableCollection的MenuMenu <MenuItem>不刷新数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.