c# - 实体数据网格: best way to add computed values?

标签 c# wpf datagrid entity

我有一个 WPF 数据网格数据绑定(bind)到 CollectionViewSource,它本身基于实体查询

例如,在我的网格中,我有 3 列 Number1、Number2、Number3,它们是实体的所有属性。如果我想添加一个名为 SumNumber 的列,它等于 Number1+Number2+Number3,那么最好的方法是什么,以便当我更改 Number1 时,SumNumber 中的值会更新?

提前致谢


我已经快到了,但我又收到了一个错误。请参阅下面的代码片段

public partial class MyEntity
{
    public double MyCustomizedProperty{get;set;}

    public MyEntity()
    {
        this.PropertyChanged += Entity_PropertyChanged;
    }

    void Entity_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        switch (e.PropertyName )
        {
            case "Date" :
                MyCustomizedProperty = DateTime.Now.Second;
                ReportPropertyChanged("MyCustomizedProperty");
                break;
        }
    }
}

可以编译,但是当我更改“日期”时,我收到运行时错误:

The property 'SigmaFinal' does not have a valid entity mapping on the entity object. For more information, see the Entity Framework documentation.

我想这是因为该属性不在 OnStateManager 中。您能告诉我如何解决这个问题吗?

谢谢

最佳答案

我要么创建一个 Converter 来获取所有 3 个值并返回它们的总和,要么扩展 Entity Framework 类以包含额外的属性

下面是扩展 EF 类以包含额外属性的示例:

public partial class MyEntity
{

    public MyEntity()
    {
        // Hook up PropertyChanged event to alert the UI
        // to update the Sum when any of the Values change
        this.PropertyChanged += MyEntity_PropertyChanged;
    }

    void MyEntity_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        switch e.PropertyName
        {
            case "Value1":
            case "Value2":
            case "Value3":
                ReportPropertyChanged("SumValues");
                break;
        }
    }


    public int SumValues
    {
        get
        {
            return Value1 + Value2 + Value3;
        }
    }

}

关于c# - 实体数据网格: best way to add computed values?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7614772/

相关文章:

wpf - 电子节目指南 (EPG) XAML

当数据源为空时,WPF DataGrid 无法添加行

c# - 如何在WPF中绑定(bind)两个DataGrid列宽度?

c# - 如何检查类型是否可以在 C# 中转换为另一种类型

c# - 任务中的 ShowDialog 方法

c# - gacutil 不被识别为内部或外部命令?

WPF/银光 : Attached Properties and Garbage Collection

php - Symfony2 - Sonata Datagrid 过滤器运算符转换对 doctrine_orm_class 字段失败

c# 从服务器 Ping 一个 TCP 客户端

c# - 在 Web API (.Net Framework) 中创建自定义 AuthorizeAttribute