实体Datagrid:添加计算值的最佳方法? [英] Entity Datagrid : best way to add computed values?

查看:140
本文介绍了实体Datagrid:添加计算值的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF datagrid数据绑定到一个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)
{
caseDate :
MyCustomizedProperty = DateTime.Now.Second;
ReportPropertyChanged(MyCustomizedProperty);
break;
}
}
}

当我更改日期时,我得到一个运行时错误:

 属性SigmaFinal没有一个有效的实体映射实体对象。有关更多信息,请参阅实体框架文档。 

我想这是因为该属性不在OnStateManager中。你可以让我知道如何解决这个问题吗?



谢谢

解决方案

p>我将创建一个 Converter ,它将采用所有3个值并返回它们的总和,或者我将扩展Entity Framework类以包含额外的属性



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

 公共部分类MyEntity 
{

public MyEntity()
{
//挂起PropertyChanged事件以提醒UI
//更新Sum当任何值更改
this.PropertyChanged + = MyEntity_PropertyChanged;
}

void MyEntity_PropertyChanged(object sender,PropertyChangedEventArgs e)
{
switch e.PropertyName
{
caseValue1:
caseValue2:
caseValue3:
ReportPropertyChanged(SumValues);
break;
}
}


public int SumValues
{
get
{
返回Value1 + Value2 + Value3 ;
}
}

}


I have a WPF datagrid databound to a collectionviewsource, which is itself based of a entity query

In my grid for example I have 3 columns Number1, Number2, Number3, all properties of an entity. If I want to add a column called SumNumber which is equal to Number1+Number2+Number3, what is the best way to do that so that when I change Number1, the value in SumNumber is updated ?

Thanks in advance


I'm almost there but I'm getting another error. See below a snippet of code

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;
        }
    }
}

That compiles and all, but when I change "Date" I get a runtime error :

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

I suppose this is due to the fact that the property is not in the OnStateManager. Can you please let me know how to fix that ?

Thanks

解决方案

I would either create a Converter that would take all 3 values and return the sum of them, or I would expand the Entity Framework class to include an extra property

Here's an example of expanding the EF class to include the extra property:

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;
        }
    }

}

这篇关于实体Datagrid:添加计算值的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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