[英]Raise Property Changed event from other class
在我的项目中,我想在用户控件上显示一个列表。 为此,我有一个CategoryView用户控件以及一个ListView控件,我想在其中显示列表。 还有CategoryViewModel。 在ViewModel上,我有一个list-property属性,同时还引发了属性change事件。
public class CategoryViewModel : NotificationObject
{
private List<string> categoryList;
public List<string> CategoryList
{
get
{
return this.categoryList;
}
set
{
this.categoryList = value;
this.RaisePropertyChanged("CategoryList");
}
}
}
该列表绑定到视图中的ListView元素。
如果我在CategoryViewModel中更改List,它可以正常工作并且引发属性更改事件。 如果我从MainWindowViewModel更改List。 没有引发属性更改事件,并且视图将不会更新。 我该怎么做?
在MainWindowViewModel上,我更改了CategoryList。 该列表将正确填写。
CategoryViewModel categoryViewModel = new CategoryViewModel();
categoryViewModel.CategoryList = logger.ReadLogfile(this.logFileName).ToList();
您似乎有些困惑。 您的CategoryView UserControl
有一个ListView
。 它的ItemsSource
属性只能是绑定到一个集合的数据,因此很明显,在主视图模型和 CategoryViewModel
更改集合时,只有一个会影响ListView
。
从您的代码看来, CategoryViewModel
被设置为UserControl
的DataContext
,因此主视图模型中的集合将不会连接到ListView
。 如果要改为将数据从ListView
绑定到主视图模型中的集合,则需要使用RelativeSource Binding
:
<ListView ItemsSource="{Binding SomeCollection, RelativeSource={RelativeSource
AncestorType={x:Type YourPrefix:YourParentType}}}" ... />
即便如此,现在CategoryViewModel
的集合将不再连接,因此您最好在此处精确地确定要执行的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.