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类(可观察对象只是实现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屋!