如何引发属性更改事件来触发其他属性

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

我不确定我的标题是否正确。我有两个双精度属性,它们绑定到 2 个文本框,并且我想在一个属性更改时计算它们的总数。 RemainingBalance 属性是我计算并显示总数的地方。我认为下面的代码将计算 UI 中的 GiftSold 或 GiftUsed 值何时发生变化。我什至尝试了双向模式,但仍然不起作用。

    private double _giftSold;
    public double GiftSold
    {
        get { return _giftSold; }
        set
        {
            if (value != _giftSold)
            {
                _giftSold = value;
                OnPropertyChanged("GiftSold");
                OnPropertyChanged("RemainingBalance");
            }
        }
    }

    private double _giftUsed;
    public double GiftUsed
    {
        get { return _giftUsed; }
        set
        {
            if (value != _giftUsed)
            {
                _giftUsed = value;
                OnPropertyChanged("GiftUsed");
                OnPropertyChanged("RemainingBalance");
            }
        }
    }

    public double RemainingBalance
    {
        get { return GiftSold - GiftUsed; }
    }

这是我的xaml

<StackPanel Orientation="Horizontal">
    <TextBlock Text="Gift Sold  "/>
    <TextBox Text="{Binding GiftSold, Mode=TwoWay}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
    <TextBlock Text="Gift Used  "/>
    <TextBox Text="{Binding GiftUsed, Mode=TwoWay}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
    <TextBlock Text="Remain Balance  "/>
    <TextBlock Text="{Binding RemainBalance}"/>
</StackPanel>
c# wpf inotifypropertychanged
2个回答
0
投票
    private double _giftSold;
public double GiftSold
{
    get { return _giftSold; }
    set
    {
        if (value != _giftSold)
        {
            _giftSold = value;
            CalculateRemainingBalance();
            OnPropertyChanged("GiftSold"); 
        }
    }
}

private double _giftUsed;
public double GiftUsed
{
    get { return _giftUsed; }
    set
    {
        if (value != _giftUsed)
        {
            _giftUsed = value;
                CalculateRemainingBalance();
            OnPropertyChanged("GiftUsed");
        }
    }
}

private double _RemainingBalance;
public double RemainingBalance
{
    get { return _RemainingBalance }
}

private void CalculateRemainingBalance()
{
_RemainingBalance = GiftSold - GiftUsed;
 OnPropertyChanged("RemainingBalance");
}

试试这个。


0
投票

如果你想绑定一个属性,它应该定义为 DependencyProperty。这是为您提供的解决方案:

    public partial class MainWindow : Window
    {
        private double _giftSold;
        public double GiftSold
        {
            get { return _giftSold; }
            set 
            { 
                _giftSold = value;
            }
        }
        public static readonly DependencyProperty GiftSoldProperty =
            DependencyProperty.Register("GiftSold",
                                        typeof(double),
                                        typeof(MainWindow),
                                        new PropertyMetadata(0.0, GiftAny_Changed));
        private static void GiftAny_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MainWindow a = d as MainWindow;
            double dummy = a.RemainingBalance; // to call the "get" method
        }

        private double _giftUsed;
        public double GiftUsed
        {
            get { return _giftUsed; }
            set 
            { 
                _giftUsed = value;
            }
        }
        public static readonly DependencyProperty GiftUsedProperty =
            DependencyProperty.Register("GiftUsed",
                                        typeof(double),
                                        typeof(MainWindow),
                                        new PropertyMetadata(0.0, GiftAny_Changed));
        private double remainingBalance;
        public double RemainingBalance
        {
            get {
                remainingBalance = (double)GetValue(GiftSoldProperty) -
                    (double)GetValue(GiftUsedProperty);
                SetValue(RemainingBalanceProperty, remainingBalance);
                return remainingBalance; 
            }
            set
            {
                remainingBalance = value;
            }
        }
        public static readonly DependencyProperty RemainingBalanceProperty =
            DependencyProperty.Register("RemainingBalance",
                                        typeof(double),
                                        typeof(MainWindow),
                                        new PropertyMetadata(0.0));
        public MainWindow()
        {
            InitializeComponent();
        }
    }

和 XAML

    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Gift Sold  "/>
            <TextBox Width="119" Text="{Binding GiftSold, ElementName=window, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Gift Used  "/>
            <TextBox Text="{Binding GiftUsed, ElementName=window, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="115"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Remain Balance  "/>
            <TextBlock Text="{Binding RemainingBalance, ElementName=window, Mode=OneWay}" Width="81"/>
        </StackPanel>
    </StackPanel>

通过使用 DependencyProperty 定义,您可以通知其他控件该属性已更改。我还使用相同的定义来处理 PropertyChanged 事件。

© www.soinside.com 2019 - 2024. All rights reserved.