如何报告实体对象的自定义(添加)计算属性已更改? [英] How to report that custom (added) Calculated Property of entity object has changed?

查看:58
本文介绍了如何报告实体对象的自定义(添加)计算属性已更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我为我的英语水平较低而道歉。

First, I apologize for my low level English writing.

我在WPF MVVM项目中使用实体框架和数据绑定。
我想知道将数据绑定到由Entity Framework生成的EntityObject的已添加计算属性的最佳方法是什么。

I use Entity Framework and data-binding in WPF MVVM project. I would like to know what is the best way to do data binding to added calculated property of EntityObject which is generated with Entity Framework.

例如:

partial class Person
{
    partial string Name...

    partial string Surname...

    public string FullName
    {
        get { return Name + Surname; }
    }
}

然后在XAML中类似... Text = {Binding FullName,Mode = Twoway}

And then in XAML something like ...Text="{Binding FullName, Mode=Twoway}"

这时我的GUI不知道何时更改属性FullName ...如何通知它?
我尝试使用ReportPropertyChanged,但是返回错误...

At this point my GUI doesn't know when property FullName is changed... how can I notify it? I've tried with ReportPropertyChanged but it return's an error...

此外,我想知道当一个绑定依赖于什么时实现绑定的最佳方法是什么更多属性...计算得出的属性或值转换器或其他内容?

Also, I wonder what is the best way to implement binding when one binding depends on more properties...calculated properties or value converters or something different?

推荐答案

您可以在构造函数中订阅PropertyChanged事件,然后如果属性名称与两个源属性中的任何一个相匹配,则引发计算出的一个属性的事件。

You could subscribe to the PropertyChanged event in the constructor and if the property name matches either of the two source properties, raise the event for the calculated one.

public Person()
{
    this.PropertyChanged += (o, e) =>
        {
            if (e.PropertyName == "Name" || e.PropertyName == "Surname") OnPropertyChanged("FullName");
        };
}

这篇关于如何报告实体对象的自定义(添加)计算属性已更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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