我有一个 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/