在链接到另一个属性和静态值的更改后更新属性

问题描述 投票:0回答:1

这是一个带有简单代码的示例:

 public class Item : INotifyPropertyChanged
 {
     public event PropertyChangedEventHandler PropertyChanged;
     //  ...

     // All items have the same bonus
     public static int bonus_sell = 0;

     // The price from the NPC, no problem with this display
     public int VendorValue
     {
           get { return vendorvalue; }
           set { vendorvalue = value; OnPropertyChanged(); }
     }

     private int vendorvalue;

     // This is the final price, calculated from one property and the static variable
     // This one is correctly displayed only at the beginning
     public int FinalPrice
     {
           get { return VendorValue+bonussell; }
     }
 }

最终价格只是

VendorPrice
+ 奖金销售。

如果

VendorValue
更改,则显示的
FinalPrice
(WPF 绑定)不会更新。

如果我更改

bonus_sell
FinalPrice
也不会显示。

当其中一个组件发生变化时,如何触发

FinalPrice
“更改”?

c# properties static
1个回答
0
投票

按照JayV的规定,我必须手动通知属性变更。

public class Item : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    //  ...
    
    // All items have the same bonus
    private static int bonus_sell = 0;

    // The price from the NPC, no problem with this display
    public int VendorValue
    {
        get { return vendorvalue; }
set { vendorvalue = value; OnPropertyChanged(); OnPropertyChanged("FinalPrice"); }
    }
    private int vendorvalue;

    // This is the final price, calculated from one property and the static variable
    // This one is correctly displayed only at the beginning
    public int FinalPrice
    {
        get { return VendorValue+bonussell; }
    }

    public void Set_Bonus_Sell(int value)
    {
        bonus_sell=value;
        OnPropertyChanged("FinalPrice");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.