未注意到它中的项何时更改(即使使用INotifyPropertyChanged)有人知道为什么这个代码不能工作吗?public class CollectionViewModel : ViewModelBase {
public ObservableCollection<EntityViewModel> ContentList
{
get { return _contentList; }
set
{
_contentList = value;
RaisePropertyChanged("ContentList");
//I want to be notified here when something changes..?
//debugger doesn't stop here when IsRowChecked is toggled
}
}}public class EntityViewModel : ViewModelBase{
private bool _isRowChecked;
public bool IsRowChecked
{
get { return _isRowChecked; }
set { _isRowChecked = value; RaisePropertyChanged("IsRowChecked"); }
}}ViewModelBase把所有东西都装上RaisePropertyChanged等等,它适用于除这个问题以外的其他一切。
3 回答
繁星点点滴滴
TA贡献1803条经验 获得超3个赞
public class CollectionViewModel : ViewModelBase{ public ObservableCollection<EntityViewModel> ContentList { get { return _contentList; } } public CollectionViewModel() { _contentList = new ObservableCollection<EntityViewModel>(); _contentList.CollectionChanged += ContentCollectionChanged; } public void ContentCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { //This will get called when the collection is changed }}
在添加、删除、更改、移动或刷新整个列表时发生。
public class CollectionViewModel : ViewModelBase{ public ObservableCollection<EntityViewModel> ContentList { get { return _contentList; } } public CollectionViewModel() { _contentList = new ObservableCollection<EntityViewModel>(); _contentList.CollectionChanged += ContentCollectionChanged; } public void ContentCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Remove) { foreach(EntityViewModel item in e.OldItems) { //Removed items item.PropertyChanged -= EntityViewModelPropertyChanged; } } else if (e.Action == NotifyCollectionChangedAction.Add) { foreach(EntityViewModel item in e.NewItems) { //Added items item.PropertyChanged += EntityViewModelPropertyChanged; } } } public void EntityViewModelPropertyChanged(object sender, PropertyChangedEventArgs e) { //This will get called when the property of an object inside the collection changes }}
ObservableCollection
CollectionChanged
PropertyChanged
慕田峪4524236
TA贡献1875条经验 获得超5个赞
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ComponentModel; using System.Collections.ObjectModel;using System.Collections.Specialized;using System.Collections;namespace somethingelse{ public class ObservableCollectionEx<T> : ObservableCollection<T> where T : INotifyPropertyChanged { // this collection also reacts to changes in its components' properties public ObservableCollectionEx() : base() { this.CollectionChanged +=new System.Collections.Specialized.NotifyCollectionChangedEventHandler (ObservableCollectionEx_CollectionChanged); } void ObservableCollectionEx_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Remove) { foreach(T item in e.OldItems) { //Removed items item.PropertyChanged -= EntityViewModelPropertyChanged; } } else if (e.Action == NotifyCollectionChangedAction.Add) { foreach(T item in e.NewItems) { //Added items item.PropertyChanged += EntityViewModelPropertyChanged; } } } public void EntityViewModelPropertyChanged(object sender, PropertyChangedEventArgs e) { //This will get called when the property of an object inside the collection changes - note you must make it a 'reset' - dunno why NotifyCollectionChangedEventArgs args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset); OnCollectionChanged(args); } }}
- 3 回答
- 0 关注
- 782 浏览
添加回答
举报
0/150
提交
取消