Home > Software design >  How to execute code when a property is changed using INotifyPropertyChanged
How to execute code when a property is changed using INotifyPropertyChanged

Time:11-02

I´ve started learning only recently so this is a newbie question. Maybe someone could help me out in regards to what I´d have to do differently for my code to work.

In short: I have a class that inherits from INotifyPropertyChanged (which I´v tried to implement according to MSDN). When I press a button I want to change a variable in this class which in turn should raise a PropertyChanged Event. When the event is raised some code should be executed.

My ValueChanged class:

    public class ValueChange : INotifyPropertyChanged
{
    public ValueChange()
    {
        _size = 1;
    }
    private int _size;

    public int Size
    {
        get
        {
            return _size;
        }
        set
        {
            _size = value;
            OnPropertyRaised("Size");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyRaised([CallerMemberName] string name = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

My event listeners:

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

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ValueChange test = new ValueChange();
        test.Size = 10;
    }

    private void PropertyChanged(object sender, PropertyChangedEventArgs args)
    {
        switch (args.PropertyName)
        {
            case "Size":
                // txtbox is just some textbox in my UI 
                txtbox.Text = "some text";
                
                // This is merely a placeholder as I´d like to be able to execute any code in here
                break;
        }
    }
}

CodePudding user response:

There are a few issues with the code.

  1. You are creating a new instance of the ValueChange class every time you click.
  2. You are not subscribing to PropertyChanged event.

Although this will fix your code, is there a reason you are using PropertyChanged here instead of executing your code directly in the Button_Click event handler? PropertyChanged is usually used when binding, it is rarely used directly as you are doing here.

public partial class MainWindow : Window
{
    ValueChange test = new ValueChange();

    public MainWindow()
    {   
        test.PropertyChanged  = PropertyChanged;
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        test.Size = 10;
    }

    private void PropertyChanged(object sender, PropertyChangedEventArgs args)
    {
        switch (args.PropertyName)
        {
            case "Size":
                // txtbox is just some textbox in my UI 
                txtbox.Text = "some text";
                
                // This is merely a placeholder as I´d like to be able to execute any code in here
                break;
        }
    }
}
  • Related