2017-01-07 10 views
2

私はRelatedPropertyは明らかにSomePropertyに依存している依存プロパティのRaisePropertyChangedを明示的に呼び出すことを避けることはできますか?

public override bool RelatedProperty 
    { 
     get { return this.SomeProperty > 0; } 
    } 

    public int SomeProperty 
    { 
     get { return this.someProperty; } 
     protected set 
     { 
      this.Set<int>(ref this.someProperty, value); 
      this.RaisePropertyChanged(nameof(this.RelatedProperty)); 
     } 
    } 

を持っています。

バインダーを更新するにはRaisePropertyChangedRelatedPropertyに、より良い方法はSomePropertyからですか?

答えて

1

SomePropertyの設定者からRelatedPropertyのRaisePropertyChangedを呼び出すよりも、バインドを更新する方が良いでしょうか?

少なくとも、MvvmLightを使用せず、プロパティを実装するための基本的なアプローチ。

あなたは機能的な方法でプロパティの変更を扱うだろうなReactiveUIなどの反応性UIフレームワーク使用していた場合:

public class ReactiveViewModel : ReactiveObject 
{ 
    public ReactiveViewModel() 
    { 
     this.WhenAnyValue(x => x.SomeProperty).Select(_ => SomeProperty > 0) 
      .ToProperty(this, x => x.RelatedProperty, out _relatedProperty); 
    } 

    private int _someProperty; 
    public int SomeProperty 
    { 
     get { return _someProperty; } 
     set { this.RaiseAndSetIfChanged(ref _someProperty, value); } 
    } 

    private readonly ObservableAsPropertyHelper<bool> _relatedProperty; 
    public bool RelatedProperty 
    { 
     get { return _relatedProperty.Value; } 
    } 
} 

あなたはReactiveUIためとクリエータポール・ベッツに関するドキュメントにこれについての詳細を読むことができますが興味のある方のブログ:

https://docs.reactiveui.net/en/fundamentals/functional-reactive-programming.html http://log.paulbetts.org/creating-viewmodels-with-reactiveobject/

関連する問題