第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

未注意到它中的項(xiàng)何時更改(即使使用INotifyPropertyChanged)

未注意到它中的項(xiàng)何時更改(即使使用INotifyPropertyChanged)

C#
慕妹3242003 2019-06-19 10:47:39
未注意到它中的項(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個贊

當(dāng)您更改集合中的值時,ContentList的set方法將不會被調(diào)用,而是應(yīng)該查找收藏變化事件觸發(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)
    {
        //This will get called when the collection is changed
    }}

好的,這是今天的兩次,我被錯誤的MSDN文檔咬了一頓。在我給你的鏈接里寫著:

在添加、刪除、更改、移動或刷新整個列表時發(fā)生。

但實(shí)際上當(dāng)一個項(xiàng)目被更改時觸發(fā)。我想你需要一種更強(qiáng)力的方法:

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
    }}

如果您非常需要這一點(diǎn),那么您可能需要自己的子類。ObservableCollection觸發(fā)CollectionChanged當(dāng)成員觸發(fā)其PropertyChanged事件自動發(fā)生(就像它在文檔中說的那樣.)


查看完整回答
反對 回復(fù) 2019-06-19
?
慕田峪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);
        }
    }}


查看完整回答
反對 回復(fù) 2019-06-19
  • 3 回答
  • 0 關(guān)注
  • 810 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號