实体数据网格:添加计算值的最佳方式?

发布于 2024-12-07 11:19:30 字数 1060 浏览 1 评论 0原文

我有一个 WPF 数据网格数据绑定到 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 中。你能让我知道如何解决这个问题吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追风人 2024-12-14 11:19:30

我要么创建一个 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;
        }
    }

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文