为级联只读属性避免重复的RaisePropertyChanged [英] Avoiding Duplicate RaisePropertyChanged for Cascading Read-Only Properties

查看:160
本文介绍了为级联只读属性避免重复的RaisePropertyChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图模型中有几个只读属性,这些属性被视图引用,其中一些依赖于一个/多个其他只读属性(在同一视图模型中),这些属性最终都取决于一个/多个读取/写入属性(在同一视图模型中).我仅在示例中看到了以下模式,以确保为所有可能受影响的属性引发PropertyChanged事件,但是我不喜欢复制所有这些RaisePropertyChanged调用.

I have several Read-Only Properties in my View Model that are referenced by the View and some of them are dependent on one/more other Read-Only Properties (in the same View Model) which are ultimately dependent on one/more Read/Write Properties (in the same View Model). I've only seen the following pattern in samples to make sure PropertyChanged Event is Raised for all potentially affected Properties, but I don't like duplicating all those RaisePropertyChanged Calls.

我怀疑我应该在我的视图模型的PropertyChanged事件中添加一个处理程序,并且在该处理程序中,对于具有依赖属性的每个属性,仅对直接 的属性调用RaisePropertyChanged (相对于间接也是间接依赖)该属性.如何避免重复所有RaisePropertyChanged通话?

I suspect I should instead be adding a Handler to the PropertyChanged Event of my View Model and in that Handler, for each Property which has dependent Properties, call RaisePropertyChanged on only the Properties that directly (vs. also the ones that indirectly) depend on that Property. How can I avoid duplicating all those RaisePropertyChanged Calls?

public bool MyReadOnlyPropertyAA1
{
    get
    {
        return MyReadOnlyPropertyA1 [&& MyReadOnlyPropertyB1 ...]
    }
}


public bool MyReadOnlyPropertyA1
{
    get
    {
        return (MyPropertyA == (Some Value)) [&& MyPropertyB == (Some Other Value)) ... ]
    }
}

public MyPropertyAType MyPropertyA
{
    get
    {
        return myPropertyA
    }
    set
    {
        myPropertyA = value;
        RaisePropertyChanged(nameof(MyPropertyA));  
        RaisePropertyChanged(nameof(MyReadOnlyPropertyA));
        RaisePropertyChanged(nameof(MyReadOnlyPropertyA1));
        RaisePropertyChanged(nameof(MyReadOnlyPropertyAA1));
        ...
        RaisePropertyChanged(nameof(MyReadOnlyPropertyAA...1));
    }
}


public MyPropertyBType MyPropertyB
{
    get
    {
        return myPropertyB
    }
    set
    {
        myPropertyB = value;
        RaisePropertyChanged(nameof(MyPropertyB));  
        RaisePropertyChanged(nameof(MyReadOnlyPropertyA));
        RaisePropertyChanged(nameof(MyReadOnlyPropertyA1));
        RaisePropertyChanged(nameof(MyReadOnlyPropertyAA1));
        ...
        RaisePropertyChanged(nameof(MyReadOnlyPropertyAA...1));
    }
 }

推荐答案

我根据属性关系使用两种方法.

I use both approaches depending on the property relationship.

如果逻辑上更改一个属性会触发另一个属性的更改是合理的,那么我只需将属性更改代码留在设置器中即可.

If it would be logical that changing one property would trigger a change in another, I just leave the property change code in the setter.

public string PropertyA
{
    get { return propertyA; }
    set
    {
        myPropertyA = value;
        RaisePropertyChanged(nameof(PropertyA));  
        RaisePropertyChanged(nameof(IsPropertyAValid));
        ...
    }
}

但是,如果两个属性在逻辑上不相关,那么我将使用PropertyChange处理程序:

However if the two properties are not logically related, then I will use the PropertyChange handler :

private void MyClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    switch(e.PropertyName)
    {
        case "PropertyA":
            RaisePropertyChanged(nameof(PropertyB));
            break;
    }
}

这样做的原因是我不需要属性设置器中的特殊逻辑.对我来说,它们应该相当通用,卡在#region标记中,然后折叠起来就被遗忘了.

The reason for this is I want no special logic in my property setters. To me, they should be fairly generic, stuck in a #region tag, and collapsed to be forgotten about.

如果两个属性在逻辑上相关,并且您希望更改一个属性可能会更改另一个属性的值,则RaisePropertyChange代码可以保留在设置器中.

If two properties are logically related and you would expect changing one to potentially alter the value of another, then the RaisePropertyChange code can be left in the setter.

但是如果它们不同,并且将来我本人或另一个开发人员正在查看代码,并且可能不知道/不了解两者之间存在依赖关系,那么我将更改放入类的PropertyChange事件处理程序中,这样很容易查找和/或必要时进行更改.

But if they are different, and in the future myself or another dev were looking at the code and would potentially not know/understand that there's a dependency between the two, I put the change in the class's PropertyChange event handler so it's easy to find and/or change if necessary.

这篇关于为级联只读属性避免重复的RaisePropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆