We are developing a UWP app using Template10. The app displays Cost, Net, Tax and Total correctly. Tax and Total are calculated properties in the ViewModel. However when Net is updated in the ViewModel, Tax and Total are updated in the ViewModel but not updated in the View. The Xaml:
<TextBlock
Text="{x:Bind ViewModel.Net,Mode=OneWay}"
/>
<TextBlock
Text="{x:Bind ViewModel.Tax,Mode=OneWay}"
/>
<TextBlock
Text="{x:Bind ViewModel.Total,Mode=OneWay}"
/>
The ViewModel:
public class ViewModel : ViewModelBase
{
decimal? _Net = default(decimal?);
public decimal? Net
{
get
{
return _Net;
}
set
{
if (value == 0) value = null;
Set(ref _Net, value);
}
}
decimal? _TaxRate = default(decimal?);
public decimal? TaxRate { get { return _TaxRate; } set { Set(ref _TaxRate, value); } }
public decimal? Tax
{
get
{
return TaxRate / 100 * Net;
}
}
public decimal? Total { get { return Net + Tax; } }
We have a command in the ViewModel that edits Net
DelegateCommand _SetDiscount;
public DelegateCommand SetDiscount
=> _SetDiscount ?? (_SetDiscount = new DelegateCommand(() =>
{
// for simplicity deleted calculations for the newNet
this.Net = newNet ?? 0;
}, () => true));
Net, Tax and Total are correctly updated in the ViewModel. Net is correctly updated in the View. Why are Tax and Total not updated in the View?
They are not updated in the View because you are not informing it about the change. Your Set()
method has inside a RaisePropertyChanged(string)
method (or something similar invoking PropertyChanged event) so your Net
value change is being shown, just add in Net
setter change information for the two other:
public decimal? Net
{
get
{
return _Net;
}
set
{
if (value == 0) value = null;
Set(ref _Net, value);
RaisePropertyChanged(nameof(Total));
RaisePropertyChanged(nameof(Tax));
}
}