WPF:ObservableCollection项目中计算字段的刷新值 [英] WPF: refreshing value of a computed field in an item of an ObservableCollection
问题描述
我有一个ObservableCollection(_additionalCosts)绑定到一个ListView,其中包含一个只读的计算字段基于其他的值项目中的属性,最重要的是另一个下拉列表的值。
当我更改所选项目的下拉列表时,我想要计算的字段刷新,它是
我的视图类看起来像这样:
[Serializable]
public class BookingView:ViewBase
{
private ObservableCollection< AdditionalCostView> _additionalCosts = new ObservableCollection< AdditionalCostView>();
//..其他成员
public ReferenceDataView SellPriceCurrency
{
get {return _sellPriceCurrency; }
set
{
this.OnPropertyChanged(SellPriceCurrency);
}
}
//..其他成员
}
AdditionalCostView的项目如下所示:
[Serializable()]
public class AdditionalCostView:ViewBase,IEquatable< AdditionalCostView>
{
//..其他成员
私有十进制数? _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)
}
返回null;
}
}
//..其他成员
}
如果您需要更多代码,请让我知道。
如何实现?
我想你的ViewBase类实现了INotifyPropertyChanged。
从你的代码我明白,每次 _charterSellPriceCurrency
或 _sellPrice
或 _buyPrice
更改,保证金
属性必须重新计算。
此代码仅考虑_ charterSellPriceCurrency
更改,但很容易将其扩展到其他字段。
private YourType _charterSellPriceCurrency;
private Nullable< decimal> _margin;
public YourType CharterSellPriceCurrency
{
get
{
return _charterSellPriceCurrency;
}
private set
{
if(value!= _charterSellPriceCurrency)
{
_charterSellPriceCurrency = value;
OnPropertyChanged(CharterSellPriceCurrency);
计算金额();
}
}
}
public Nullable< decimal> Margin
{
get
{
return _margin;
}
private set
{
if(value!= _margin)
{
_margin = value;
OnPropertyChanged(Margin);
}
}
}
private void CalculateMargin()
{
if(_charterSellPriceCurrency == null)
{
Margin = null;
return;
}
if(_sellPrice.HasValue&& _buyPrice.HasValue)
{
Margin = _exchangeRateConverter.ConvertUsingExchangeRate(1,_sellPriceCurrency.Name,_charterSellPriceCurrency.Name ,_sellPrice.Value) -
_exchangeRateConverter.ConvertUsingExchangeRate(1,_buyPriceCurrency.Name,_charterSellPriceCurrency.Name,_buyPrice.Value);
return;
}
Margin = null;
}
重点是您必须通知UI保证金属性已更改,无论是只读属性,如果更改,您必须提出事件。否则,用户界面无法察觉到它已被重新计算。
I have an issue with my WPF ObservableCollection.
I have an ObservableCollection (_additionalCosts) bound to a ListView which includes one readonly computed field based on the value of other properties in the item and, crucially, the value of another dropdown.
When I change the selected item of the dropdown I would like the computed field to refresh, which it is not doing at the moment.
My view class looks like this:
[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
}
The items of the AdditionalCostView looks like this:
[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
}
If you need any more code, please let me know.
How do I accomplish this?
I suppose that your ViewBase class implements INotifyPropertyChanged.
From your code I understand that everytime that _charterSellPriceCurrency
or _sellPrice
or _buyPrice
changes, the Margin
property has to be recalculated.
This code consider just the _charterSellPriceCurrency
changings, but it is easy to extend it to the other fields.
private YourType _charterSellPriceCurrency;
private Nullable<decimal> _margin;
public YourType CharterSellPriceCurrency
{
get
{
return _charterSellPriceCurrency;
}
private set
{
if (value != _charterSellPriceCurrency)
{
_charterSellPriceCurrency = value;
OnPropertyChanged("CharterSellPriceCurrency");
CalculateMargin();
}
}
}
public Nullable<decimal> Margin
{
get
{
return _margin;
}
private set
{
if (value != _margin)
{
_margin = value;
OnPropertyChanged("Margin");
}
}
}
private void CalculateMargin()
{
if (_charterSellPriceCurrency == null)
{
Margin = null;
return;
}
if (_sellPrice.HasValue && _buyPrice.HasValue)
{
Margin = _exchangeRateConverter.ConvertUsingExchangeRate(1, _sellPriceCurrency.Name, _charterSellPriceCurrency.Name, _sellPrice.Value) -
_exchangeRateConverter.ConvertUsingExchangeRate(1, _buyPriceCurrency.Name, _charterSellPriceCurrency.Name, _buyPrice.Value);
return;
}
Margin = null;
}
The point is that you must notify to the UI that the Margin property changed, no matter if it is a read only property, if it changes you have to raise the event. Otherwise the UI can't be aware that it was recalculated.
这篇关于WPF:ObservableCollection项目中计算字段的刷新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!