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

zu0ti5jz  于 2023-10-22  发布在  其他
关注(0)|答案(2)|浏览(155)

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

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>
aiqt4smr

aiqt4smr1#

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");
}

试试这个.

llmtgqce

llmtgqce2#

如果你想绑定一个属性,它应该定义为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事件。

相关问题