未注意到它中的項(xiàng)何時更改(即使使用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 回答

繁星點(diǎn)點(diǎn)滴滴
TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超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 }}
在添加、刪除、更改、移動或刷新整個列表時發(fā)生。
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貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超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 關(guān)注
- 810 瀏覽
添加回答
舉報(bào)
0/150
提交
取消