2012-01-14 25 views
0

私はMVVMスタイルのアプリケーションを持っています。 ObservableCollectionAを保持する依存性プロパティがあるカスタムコントロールがあります.AはObservableCollectionBを持ちます.AとBはINotifyPropertyChangedを実装しています。MVVM、多次元プロパティ、要素の変更を反映

ViewModelでBのオブジェクトをAに追加すると、変更はコントロールに反映されません。 Bsが表示されModeがTwoWayなので、xamlのバインディングは正しいです。

答えて

1

コントロールはINotifyPropertyChangedでのみ受信し、ICollectionChangedでは受信しないため、これは正常な動作です。コレクションプロパティ自体は変更されないため、コントロールはリフレッシュする必要があることを認識しません。

コレクションの変更をコントロールに渡すには、CollectionChangedイベントの登録をObservableCollectionにしてから、コレクションが変更されたときにコレクションプロパティのプロパティ変更イベントを発生させる必要があります(アイテムの追加、削除、移動、またはコレクションがクリアされた場合)。

#region [BViewModelCollection] 

/// <summary> 
/// The <see cref="BViewModelCollection" /> property's name. 
/// </summary> 
public const string BViewModelCollectionPropertyName = "BViewModelCollection"; 

private ObservableCollection<BViewModel> _bViewModelCollection = new ObservableCollection<BViewModel>(); 

/// <summary> 
/// Gets the BViewModelCollection property. 
/// TODO Update documentation: 
/// Changes to that property's value raise the PropertyChanged event. 
/// This property's value is broadcasted by the Messenger's default instance when it changes. 
/// </summary> 
public ObservableCollection<BViewModel> BViewModelCollection { 
    get { 
     return _bViewModelCollection; 
    } 

    set { 
     if (_bViewModelCollection != value) { 
      SetBViewModelCollection(value); 

      RaisePropertyChanged(BViewModelCollectionPropertyName); 
     } 
    } 
} 

private void SetBViewModelCollection(ObservableCollection<BViewModel> value) { 
    if (_bViewModelCollection != null) 
     _bViewModelCollection.CollectionChanged -= this.BViewModelCollection_CollectionChanged; 

    _bViewModelCollection = value; 

    if (_bViewModelCollection != null) 
     _bViewModelCollection.CollectionChanged += this.BViewModelCollection_CollectionChanged; 
} 

private void BViewModelCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { 
    RaisePropertyChanged(BViewModelCollectionPropertyName); 
} 

#endregion 

あなたは今ObservableCollectionを設定し、自動的に収集するためにPropertyChangedイベントを上昇させることなく、イベントを正しく登録するSetBViewModelCollectionを使用することができます - 例えばコンストラクタまたはロードデータメソッドでの使用のために。

アイテムがコレクションに追加されたり、コレクションから削除されたりすると、コレクションの推移が変更されたことをコントロールに通知する必要があります。

:メソッド名を独自のフレームワークに調整します。

+0

ご連絡ありがとうございました!私はそれを試してみましょう! – user1149223

関連する問題