MVVMで次の簡単な式で「NetAmount」を計算しようとしています
GrossAmount+Carriage-割引=NetAmount
私はMVVMLightToolkitを使用しており、次のようにプロパティを宣言しています
public const string DiscountPropertyName = "Discount";
private double _discount;
public double Discount
{
get
{
return _discount;
}
set
{
if (_discount == value)
{
return;
}
_discount = value;
// Update bindings, no broadcast
RaisePropertyChanged(DiscountPropertyName);
}
}
public const string CarriagePropertyName = "Carriage";
private double _carriage;
public double Carriage
{
get
{
return _carriage;
}
set
{
if (_carriage == value)
{
return;
}
_carriage = value;
RaisePropertyChanged(CarriagePropertyName);
}
}
public const string NetAmountPropertyName = "NetAmount";
private double _netAmount;
public double NetAmount
{
get
{
_netAmount = Carriage + Discount;
return _netAmount;
}
set
{
if (_netAmount == value)
{
return;
}
_netAmount = value;
RaisePropertyChanged(NetAmountPropertyName);
}
}
public const string GrossAmountPropertyName = "GrossAmount";
private double _grossAmount;
public double GrossAmount
{
get
{
return _grossAmount;
}
set
{
if (_grossAmount == value)
{
return;
}
_grossAmount = value;
RaisePropertyChanged(GrossAmountPropertyName);
}
}
これらのプロパティをXAMLで次のようなテキストボックスにバインドします。
<TextBox Text="{Binding GrossAmount, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding Mode=OneWay}"/>
<TextBox Text="{Binding Carriage, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding Mode=OneWay}"/>
<TextBox Text="{Binding Discount, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding Mode=OneWay}"/>
NetAmount
そして、次のようにテキストブロックをプロパティにバインドします。
<TextBlock Text="{Binding NetAmount}" />
ViewModelはSalesOrderViewModel
です。
NetAmount
テキストボックスの値のいずれかが変更されたときにプロパティが変更されるように、上記の式をどこに配置すればよいかわかりません。
私はC#に不慣れではありませんが、MVVMとPropertyChanged
イベントに不慣れです。私が間違っている非常に小さな愚かなことがいくつかあることを知っていますが、頭を悩ませることはできません。
どんな助けでも大歓迎です。