I have model and viewmodel in my WPF app simple as shown below.
My intention is to update HumanizedPrice
property with some string from WCF when GivenPrice
is changed. HumanizedPrice
is based on GivenPrice
and WCF is needed to get it.
What is the best MVVM-compatibile approach for this?
Model:
class Price : INotifyPropertyChanged
{
public string GivenPrice
{
get
{
return _givenPrice;
}
set
{
if (_givenPrice != value)
{
_givenPrice = value;
RaisePropertyChanged("GivenPrice");
RaisePropertyChanged("HumanizedPrice");
}
}
}
public string HumanizedPrice
{
get
{
return _humanizedPrice;
}
set
{
if (_humanizedPrice != value)
{
_humanizedPrice = value;
RaisePropertyChanged("HumanizedPrice");
}
}
}
private string _givenPrice;
private string _humanizedPrice;
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
ViewModel:
class PriceViewModel
{
public PriceViewModel()
{
LoadPrices();
}
public ObservableCollection<Price> Prices
{
get;
set;
}
public void LoadPrices()
{
ObservableCollection<Price> prices = new ObservableCollection<Price>();
prices.Add(new Price { GivenPrice = "221 331,44" });
prices.Add(new Price { GivenPrice = "2 331,44" });
prices.Add(new Price { GivenPrice = "331,44" });
prices.Add(new Price { GivenPrice = "0,44" });
Prices = prices;
}
}