编程语言
首页 > 编程语言> > c# – 计算Property Changed MVVM上两个值的结果

c# – 计算Property Changed MVVM上两个值的结果

作者:互联网

我试图通过MVVM中的以下简单公式计算“NetAmount”

GrossAmount + Carriage – Discount = NetAmount

我正在使用MVVM Light Toolkit和声明的属性如下

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 Events的新手,我知道有一些非常小的傻事我做错了但是无法理解它.

任何帮助将受到高度赞赏.

解决方法:

由于NetAmount是一种计算,因此将其建模为视图模型中的只读属性是有意义的.访问该属性实际上执行计算.最后一个技巧是每当影响NetAmount的任何因素发生变化时调用RaisePropertyChanged(NetAmountProperty)

public const string GrossAmountPropertyName = "GrossAmount";
private double _grossAmount;
public double GrossAmount
{
    get { return _grossAmount; }
    set 
    {
           if (_grossAmount == value)
                return;

           RaisePropertyChanged(GrossAmountPropertyName);
           RaisePropertyChanged(NetAmountPropertyName);
    }
}

public double Discount{} ... //Implement same as above
public double Carriage {} ... //Implement same as above

public const string NetAmountPropertyName = "NetAmount";
public double NetAmount
{
    get { return GrossAmount + Carriage - Discount; }
}

编辑:如果您不想向每个影响NetAmount的属性添加对RaisePropertyChanged的调用,则可以修改RaisePropertyChanged,以便它还为NetAmount属性引发PropertyChanged事件.它将导致一些不必要的PropertyChanged事件被引发但更易于维护

private void RaisePropertyChanged(string propertyName)
{
     var handler = PropertyChanged;
     if (handler != null)
     {
          handler(this, new PropertyChangedEventArgs(propertyName);
          handler(this, new PropertyChangedEventArgs(NetAmountProperty);
     }
}

标签:c,mvvm,silverlight,propertychanged
来源: https://codeday.me/bug/20190620/1249544.html