我正在使用Entity框架构建C#应用程序。
我有一个从我的数据库创建的实体,其中包含“Price”和“Quantity”字段。
使用部分类声明我创建了一个自定义属性“SubTotal”,如下所示:
partial class Detail {
public decimal Subtotal {
get
{
return Price * Quantity;
}
}
}
问题是,我在我的应用程序中使用了很多DataBinding。
在某处,我使用IBindingList<Detail>
作为ItemsSource
用于ListView。
当我修改(使用代码隐藏)列表中的一些元素,数量和价格时,列表视图会正确更新。
但是,小计不会更新。
我认为这是因为某些内容是由实体框架自动完成的,以通知某些内容已被更改,但我不知道要添加到我的自定义属性中,以便它的行为方式相同。
你可以帮帮我吗?答案 0 :(得分:2)
用户界面不知道Subtotal
的值已更改。实施System.ComponentModel.INotifyPropertyChanged
,然后举起PropertyChanged
事件,让用户知道在Subtotal
或Price
发生变化时,Quantity
已更改。
例如:
partial class Detail : INotifyPropertyChanged {
public decimal Subtotal
{
get
{
return Price * Quantity;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
然后,当Price
或Quantity
更改时,您需要调用NotifyPropertyChanged("Subtotal")
,并且UI应该适当更新显示的Subtotal
值。
答案 1 :(得分:2)
对我有用的是调用OnPropertyChanged方法:
OnPropertyChanged("CustomPropertyName")