ObservableCollection 不更新视图 [英] ObservableCollection not updating View
问题描述
我刚刚开始使用 MVVM,遇到了一个障碍,希望有人能帮助我.我正在尝试使用 2 个列表框创建一个简单的视图.从第一个列表框中选择的内容将填充第二个列表框.我创建了一个类来存储我想要绑定的信息.
I am just starting with MVVM and have hit a hurdle that I hope someone can help me with. I am trying to create a simple View with 2 listboxes. A selection from the first listbox will populate the second list box. I have a class created that stores the information I want to bind to.
MyObject 类(Observable Object 只是一个实现 INotifyPopertyChanged 的基类)
MyObject Class (Observable Object is just a base class that implements INotifyPopertyChanged)
public class MyObject : ObservableObject
{
String _name = String.Empty;
ObservableCollection<MyObject> _subcategories;
public ObservableCollection<MyObject> SubCategories
{
get { return _subcategories; }
set
{
_subcategories = value;
RaisePropertyChanged("SubCategories");
}
}
public String Name
{
get { return _name; }
set
{
_name = value;
RaisePropertyChanged("Name");
}
}
public MyObject()
{
_subcategories = new ObservableCollection<EMSMenuItem>();
}
}
在我的视图模型中,我创建了两个 ObservableCollections
In my viewmodel I have two ObservableCollections created
public ObservableCollection<EMSMenuItem> Level1MenuItems { get; set; }
public ObservableCollection<EMSMenuItem> Level2MenuItems { get; set; }
在我的 ViewModel 构造函数中,我有:
In my constructor of the ViewModel I have:
this.Level1MenuItems = new ObservableCollection<EMSMenuItem>();
this.Level2MenuItems = new ObservableCollection<EMSMenuItem>();
this.Level1MenuItems = LoadEMSMenuItems("Sample.Xml");
这适用于 Level1 项目并且它们正确显示在视图中.但是,当用户单击列表框中的项目时,我有一个命令会被调用,该命令具有以下内容:
That works fine for the Level1 items and they correctly show in the View. However I have a command that gets called when the user clicks an item in the listbox, which has the following:
Level2MenuItems = ClickedItem.SubCategories;
出于某种原因,这不会更新第二个列表框的 UI.如果我在这个位置放置一个断点,我可以看到 Level2MenuItems 中存储了正确的信息.如果我编写了一个 foreach 循环并将它们单独添加到 Level2MenuItems 集合中,那么它确实可以正确显示.
For some reason this does not update the UI of the second listbox. If I put a breakpoint at this location I can see that Level2MenuItems has the correct information stored in it. If I write a foreach loop and add them individually to the Level2MenuItems collection then it does display correctly.
同样作为测试,我在构造函数中添加了以下内容:
Also as a test I added the following to the constructor:
Level2MenuItems = Level1MenuItems[0].SubCategories;
而且更新正确.
那么为什么代码在构造函数中或在循环时会按预期工作,而当用户单击列表框中的项目时却不会?
So why would the code work as expected in the constructor, or when looping through, but not when a user clicks on an item in the listbox?
推荐答案
您需要在 Level2MenuItems
属性上引发更改通知.
You need to raise the change notification on the Level2MenuItems
property.
取而代之的是
public ObservableCollection<EMSMenuItem> Level2MenuItems { get; set; }
你需要
private ObservableCollection<EMSMenuItem> _level2MenuItems;
public ObservableCollection<EMSMenuItem> Level2MenuItems
{
get { return _level2MenuItems; }
set
{
_level2MenuItems = value;
RaisePropertyChanged("Level2MenuItems");
}
}
前者在构造函数中起作用的原因是绑定尚未发生.但是,由于您正在通过绑定后发生的命令执行更改引用,因此您需要告诉视图它已更改
The reason the former works in the constructor is that the Binding has not taken place yet. However since you are changing the reference via a command execute which happens after the binding you need to tell view that it changed
这篇关于ObservableCollection 不更新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!