【发布时间】:2015-01-14 14:54:48
【问题描述】:
我的 WPF ObservableCollection 有问题。
我有一个绑定到 ListView 的 ObservableCollection (_additionalCosts),其中包括一个基于项目中其他属性的值的只读计算字段,并且至关重要的是,另一个下拉列表的值。
当我更改下拉列表中的选定项时,我希望刷新计算字段,目前它没有这样做。
我的视图类如下所示:
[Serializable]
public class BookingView : ViewBase
{
private ObservableCollection<AdditionalCostView> _additionalCosts = new ObservableCollection<AdditionalCostView>();
//..other members
public ReferenceDataView SellPriceCurrency
{
get { return _sellPriceCurrency; }
set
{
this.OnPropertyChanged("SellPriceCurrency");
}
}
//..other members
}
AdditionalCostView 的项目如下所示:
[Serializable()]
public class AdditionalCostView : ViewBase, IEquatable<AdditionalCostView>
{
//..other members
private decimal? _margin;
public decimal? Margin
{
get
{
if (_charterSellPriceCurrency == null)
return null;
if (_sellPrice.HasValue && _buyPrice.HasValue)
{
return _exchangeRateConverter.ConvertUsingExchangeRate(1, _sellPriceCurrency.Name, _charterSellPriceCurrency.Name, _sellPrice.Value) -
_exchangeRateConverter.ConvertUsingExchangeRate(1, _buyPriceCurrency.Name, _charterSellPriceCurrency.Name, _buyPrice.Value);
}
return null;
}
}
//..other members
}
如果您需要更多代码,请告诉我。
我该如何做到这一点?
【问题讨论】:
-
您的计算域是否实现了 INotifyPropertyChanged?也许您可以发布一些代码?
-
好的,我已经添加了一些代码
-
只需在
AdditionalCostView类中实现INotifyPropertyChanged并从其他相关属性中调用显示属性的名称。 -
你能指导我看一个如何实现这个的简洁例子吗?我有点害怕 INotifyPropertyChanged 已经三年没有做 WPF了!
-
INotifyPropertyChanged 几乎是 WPF 中最不可怕的部分。 :) 你可以做到!
标签: c# wpf data-binding observablecollection