我可以避免为依赖属性显式调用 RaisePropertyChanged 吗?

Can I avoid explicitly calling RaisePropertyChanged for dependent properties?

我有

    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.

有没有比从 SomeProperty 的 setter 为 RelatedProperty 调用 RaisePropertyChanged 更好的更新绑定的方法?

Is there a better way to update the binding, than invoking RaisePropertyChanged for RelatedProperty, from the setter of SomeProperty?

没有。至少不使用 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 的文档和创建者 Paul Betts 的博客上阅读更多相关信息:

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