【问题标题】:Entity Datagrid : best way to add computed values?实体数据网格:添加计算值的最佳方法?
【发布时间】:2011-10-03 15:56:41
【问题描述】:

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

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

提前致谢


我快到了,但我遇到了另一个错误。见下面一段sn-p代码

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 中。你能告诉我如何解决这个问题吗?

谢谢

【问题讨论】:

    标签: c# wpf datagrid entity


    【解决方案1】:

    我会创建一个 Converter 来获取所有 3 个值并返回它们的总和,或者我会扩展实体框架类以包含一个额外的属性

    下面是扩展 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;
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多