2017-01-07 115 views
2

我可以避免顯式調用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)); 
     } 
    } 

其中RelatedProperty顯然是依賴於SomeProperty

有沒有更好的方法來更新綁定,比調用RaisePropertyChangedRelatedProperty,從設置的SomeProperty

回答

1

有沒有更好的方法來更新綁定,比從SomeProperty的setter調用RaisePropertyChanged for RelatedProperty?

不,至少沒有使用MvvmLight和實現屬性的必要方法。

如果您使用的是無功UI框架如ReactiveUI你會處理的功能性的方式屬性更改:

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/