c# - 属性取决于另一类的属性

标签 c# mvvm windows-phone-8 inotifypropertychanged fody-propertychanged

我有一个使用 Fody 将 INotifyPropertyChanged 注入(inject)属性的 Windows Phone 8 应用程序。
我有属性 A 的 Class First,它绑定(bind)到 View 中的文本框:

[ImplementPropertyChanged]
public class First
{
    public int A { get; set; }

    public int AA { get {return A + 1; } }
}

第二类属性 B 取决于属性 A(也绑定(bind)到文本框):
[ImplementPropertyChanged]
public class Second
{
    private First first;

    public int B { get {return first.A + 1; } }
}

更新 A 和 AA 工作正常,但是当 first.A 更改时 B 不会自动更新。是否有一种简单而干净的方法可以使用 fody 实现这种自动更新,还是我必须创建自己的事件来处理它?

最佳答案

我对 Fody 不熟悉,但我怀疑这是因为 Second.B 上没有二传手。 Second 应该订阅 First 中的更改,如果 First.A 是被更改的属性,那么应该使用 B 的(私有(private))setter。

或者订阅 First 然后调用 B 属性更改事件:

[ImplementPropertyChanged]
public class Second
{
    private First first;

    public int B { get {return first.A + 1; } }

    public Second(First first)
    {
        this.first = first;
        this.first.OnPropertyChanged += (s,e) =>
        {
            if (e.PropertyName == "A") this.OnPropertyChanged("B");
        }
}

关于c# - 属性取决于另一类的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20940874/

相关文章:

c# - 使用 IoC 进行生产和测试的最佳方式

windows-phone-7 - 全景页面有多少个 View 模型

image - LongListSelector 项模板中图像控件的 ChangePropertyAction

.net - 在 Windows Phone 8 中使用 WCF 服务 : Async methods?

wpf - 在 WPF MVVM 中动态生成工具提示的 ViewModel

windows-phone-8 - Windows 通用应用程序的应用程序配置文件

c# - 使用 Objective Sharpie 将 Spotify iOS SDK 绑定(bind)到 C#

c# - 从控件的构造函数中检测设计模式

c# - 需要 TPL 库的典型问题有哪些?

MVVM View 事件 Viewmodel 命令绑定(bind)