WPF/INotifyPropertyChanged, change the value of txtA, the txtB and txtC should change automatically?

Posted by user1033098 on Stack Overflow See other posts from Stack Overflow or by user1033098
Published on 2012-06-22T03:11:52Z Indexed on 2012/06/22 3:16 UTC
Read the original article Hit count: 145

Filed under:
|

I supposed, once i change the value of txtA, the txtB and txtC would change automatically, since i have implemented INotifyPropertyChanged for ValueA.

But they were not updated on UI. txtB was always 100, and txtC was always -50.

I don't know what's the reason.

My Xaml..

    <Window x:Class="WpfApplicationReviewDemo.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <StackPanel>
            <TextBox Name="txtA" Text="{Binding ValueA}" />
            <TextBox Name="txtB" Text="{Binding ValueB}" />
            <TextBox Name="txtC" Text="{Binding ValueC}" />
        </StackPanel>
    </Window>

My code behind...

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = new Model();
    }
}


public class Model : INotifyPropertyChanged
{
    private decimal valueA;
    public decimal ValueA { get {
        return valueA;
    }
        set
        {
            valueA = value;
            PropertyChanged(this, new PropertyChangedEventArgs("ValueA"));
        }
    }

    private decimal valueB;
    public decimal ValueB
    {
        get
        {
            valueB = ValueA + 100;
            return valueB;
        }
        set
        {
            valueB = value;
            PropertyChanged(this, new PropertyChangedEventArgs("ValueB"));
        }
    }


    private decimal valueC;
    public decimal ValueC
    {
        get
        {
            valueC = ValueA - 50;
            return valueC;
        }
        set
        {
            valueC = value;
            PropertyChanged(this, new PropertyChangedEventArgs("ValueC"));
        }
    }


    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

After i add code to the set method of ValueA property, it works.

    public decimal ValueA { get {
        return valueA;
    }
        set
        {
            valueA = value;
            PropertyChanged(this, new PropertyChangedEventArgs("ValueA"));
            PropertyChanged(this, new PropertyChangedEventArgs("ValueB"));
            PropertyChanged(this, new PropertyChangedEventArgs("ValueC"));
        }
    }

But I supposed it should be automatically refresh/updated for txtB and txtC. Please advise.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about inotifypropertychanged