猿问

未注意到它中的项何时更改(即使使用INotifyPropertyChanged)

未注意到它中的项何时更改(即使使用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等等,它适用于除这个问题以外的其他一切。


慕妹3242003
浏览 783回答 3
3回答

繁星点点滴滴

当您更改集合中的值时,ContentList的set方法将不会被调用,而是应该查找收藏变化事件触发。public&nbsp;class&nbsp;CollectionViewModel&nbsp;:&nbsp;ViewModelBase{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;ObservableCollection<EntityViewModel>&nbsp;ContentList &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get&nbsp;{&nbsp;return&nbsp;_contentList;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;CollectionViewModel() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_contentList&nbsp;=&nbsp;new&nbsp;ObservableCollection<EntityViewModel>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_contentList.CollectionChanged&nbsp;+=&nbsp;ContentCollectionChanged; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;ContentCollectionChanged(object&nbsp;sender,&nbsp;NotifyCollectionChangedEventArgs&nbsp;e) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//This&nbsp;will&nbsp;get&nbsp;called&nbsp;when&nbsp;the&nbsp;collection&nbsp;is&nbsp;changed &nbsp;&nbsp;&nbsp;&nbsp;}}好的,这是今天的两次,我被错误的MSDN文档咬了一顿。在我给你的链接里写着:在添加、删除、更改、移动或刷新整个列表时发生。但实际上不当一个项目被更改时触发。我想你需要一种更强力的方法:public&nbsp;class&nbsp;CollectionViewModel&nbsp;:&nbsp;ViewModelBase{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;ObservableCollection<EntityViewModel>&nbsp;ContentList &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get&nbsp;{&nbsp;return&nbsp;_contentList;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;CollectionViewModel() &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_contentList&nbsp;=&nbsp;new&nbsp;ObservableCollection<EntityViewModel>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_contentList.CollectionChanged&nbsp;+=&nbsp;ContentCollectionChanged; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;ContentCollectionChanged(object&nbsp;sender,&nbsp;NotifyCollectionChangedEventArgs&nbsp;e) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(e.Action&nbsp;==&nbsp;NotifyCollectionChangedAction.Remove) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach(EntityViewModel&nbsp;item&nbsp;in&nbsp;e.OldItems) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Removed&nbsp;items &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.PropertyChanged&nbsp;-=&nbsp;EntityViewModelPropertyChanged; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if&nbsp;(e.Action&nbsp;==&nbsp;NotifyCollectionChangedAction.Add) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach(EntityViewModel&nbsp;item&nbsp;in&nbsp;e.NewItems) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Added&nbsp;items &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.PropertyChanged&nbsp;+=&nbsp;EntityViewModelPropertyChanged; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;EntityViewModelPropertyChanged(object&nbsp;sender,&nbsp;PropertyChangedEventArgs&nbsp;e) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//This&nbsp;will&nbsp;get&nbsp;called&nbsp;when&nbsp;the&nbsp;property&nbsp;of&nbsp;an&nbsp;object&nbsp;inside&nbsp;the&nbsp;collection&nbsp;changes &nbsp;&nbsp;&nbsp;&nbsp;}}如果您非常需要这一点,那么您可能需要自己的子类。ObservableCollection触发CollectionChanged当成员触发其PropertyChanged事件自动发生(就像它在文档中说的那样.)

慕田峪4524236

这使用了上述想法,但使其成为派生的“更敏感”的集合:using&nbsp;System;using&nbsp;System.Collections.Generic;using&nbsp;System.Linq;using&nbsp;System.Text;using&nbsp;System.ComponentModel; using&nbsp;System.Collections.ObjectModel;using&nbsp;System.Collections.Specialized;using&nbsp;System.Collections;namespace&nbsp;somethingelse{ &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;class&nbsp;ObservableCollectionEx<T>&nbsp;:&nbsp;ObservableCollection<T>&nbsp;where&nbsp;T&nbsp;:&nbsp;INotifyPropertyChanged &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;this&nbsp;collection&nbsp;also&nbsp;reacts&nbsp;to&nbsp;changes&nbsp;in&nbsp;its&nbsp;components'&nbsp;properties &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;ObservableCollectionEx()&nbsp;:&nbsp;base() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.CollectionChanged&nbsp;+=new&nbsp;System.Collections.Specialized.NotifyCollectionChangedEventHandler &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ObservableCollectionEx_CollectionChanged); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;ObservableCollectionEx_CollectionChanged(object&nbsp;sender,&nbsp;System.Collections.Specialized.NotifyCollectionChangedEventArgs&nbsp;e) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(e.Action&nbsp;==&nbsp;NotifyCollectionChangedAction.Remove) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Added&nbsp;items &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.PropertyChanged&nbsp;+=&nbsp;EntityViewModelPropertyChanged; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;EntityViewModelPropertyChanged(object&nbsp;sender,&nbsp;PropertyChangedEventArgs&nbsp;e) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//This&nbsp;will&nbsp;get&nbsp;called&nbsp;when&nbsp;the&nbsp;property&nbsp;of&nbsp;an&nbsp;object&nbsp;inside&nbsp;the&nbsp;collection&nbsp;changes&nbsp;-&nbsp;note&nbsp;you&nbsp;must&nbsp;make&nbsp;it&nbsp;a&nbsp;'reset'&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;dunno&nbsp;why &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NotifyCollectionChangedEventArgs&nbsp;args&nbsp;=&nbsp;new&nbsp;NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OnCollectionChanged(args); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}}
随时随地看视频慕课网APP
我要回答