2011-02-02 6 views
1

ViewModelプロパティにオブジェクトを追加する際に、RaisePropertyChangedで奇妙な動作が発生していることに気付きました。私は、プロパティを介して、コレクションにオブジェクトを追加し、これまでViewModelプロパティの不規則な動作、RaisePropertyChangedが実行されていない

Items.Add("new string"); 

が呼ばれることは決してありませんRaisePropertyChanged

private List<string> _items; 
public List<string> Items 
{ 
    get 
    { 
     if(_items == null){ _items = new List<string>(); } 
     return _itmes; 
    } 
    set 
    { 
     _items = value; 
     RaisePropertyChanged("Items"); 
    } 
} 

RaisePropertyChangedを私が望むように機能させる最も良い方法は何ですか?

答えて

5

コレクションの内容が変更された場合ではなく、コレクションを変更するとセッターが呼び出されます。 必要なものは、変更について通知するコレクションです。 ObservableCollection<T>クラスを見てください。変更について通知されたbeeingについては、CollectionChangedイベントに登録してください。セッター
次の例で

プロパティを使用すると、監視可能なコレクションを保持している設定可能なプロパティを使用して作業することができます方法を示しています。この例の複雑さは、インスタンスのアウトサイトからコレクションを設定できるためです。この機能が必要ない場合、ソリューションはずっと簡単になります。セッターのない

private ObservableCollection<string> _items; 
public ObservableCollection<string> Items { 
    get { 
     if (_items == null) { 
      // Create a new collection and subscribe to the event 
      Items=new ObservableCollection<string>(); 
     } 
     return _items; 
    } 
    set { 
     if(value !=_items){ 
     if (null != _items) { 
      // unsubscribe for the old collection 
      _items.CollectionChanged -= new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_items_CollectionChanged); 
     } 
     _items = value; 
     if (null != _items) { 
      // subscribe for the new collection 
      _items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_items_CollectionChanged);  
     } 
     // Here you will be informed, when the collection itselfs has been changed 
     RaisePropertyChanged("Items"); 
    } 
    } 
} 

void _items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { 
    // Here you will be informed, if the content of the collection has been changed. 
} 

プロパティ
あなたは、コレクションの設定可能なを持つコレクションを作成しながら、CollectionChangedに登録する必要がない場合。しかし、あなたはあなたの財産のセッターを削除する必要があります。コレクションが変更された場合にはそうでない場合は、変更が通知されません。

private ObservableCollection<string> _items; 
public ObservableCollection<string> Items { 
    get { 
     if (_items == null) { 
      // Create a new collection and subscribe to the event 
      _items=new ObservableCollection<string>(); 
      _items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler 
     } 
     return _items; 
    } 
} 

void _items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { 
    // Here you will be informed, if the content of the collection has been changed. 
} 

追加情報
コレクションの変更の詳細についてはINotifyCollectionChangedを見てください。上記の例では、IEnumerable<string>INotifyCollectionChangedで一般的に使用することもできます。

2

もちろん、アイテムをコレクションに追加するときに実際にプロパティを設定していない(Itemsプロパティのsetterが呼び出されないため)ことはありません。

コレクションの変更が必要な場合は、CollectionChangedイベントを発生させる必要があります。あなたはいつもList<T>の代わりにObservableCollection<T>を使用する必要がある。この目的のために:あなたは、コレクションに項目を追加するとき

private ObservableCollection<string> _items; 
public ObservableCollection<string> Items 
{ 
    get 
    { 
     if(_items == null){ _items = new ObservableCollection<string>(); } 
     return _itmes; 
    } 
    set 
    { 
     _items = value; 
     RaisePropertyChanged("Items"); 
    } 
} 

CollectionChangedイベントが発生しますと、あなたのUIはそれに応じて更新されます。

関連する問題