C# – Using INotifyPropertyChanged in WPF

bindingcinotifypropertychangedwpf

Hi i am trying to use the NotifyPropertyChanged to update all the places where i am binding a property. For what i have searched the INotifyPropertyChanged is indicated to this cases.

So i need help because i don't understand what i have wrong in here. And i really don't know what to do with the PropertyChange event. My question about that is, when he changes? What i have to more with him?

Datagrid:

<ListView Name="listView" ItemsSource="{Binding Categories}"/>

An example when i change my Categories property:

DataTest dtTest = new DataTest();

public MainWindow()
{
    InitializeComponent();
    this.DataContext = dtTest;
}

private void Button1_Click(object sender, RoutedEventArgs e)
{
    //Here i pick up a string from a textBox, where i will insert in a Table of my DB,
    //Then i will do a query to my Table, and i will get a DataTable for example
    //Then i just put the contents into the DataView, so the value have changed.
    dtTest.Categories = dtTable.DefaultView;
    dtTest = dtTable.defaultView; (this only an example, i don't this for real.)
    //What i have to do now, to wherever i am binding (DataGrid, ListView, ComboBox)
    //the property to the new values automatically being showed in all places?
}

My Class:

public class DataTest : INotifyPropertyChanged
{
    private DataView categories;

    public event PropertyChangedEventHandler PropertyChanged; //What i have to do with this?

    private void NotifyPropertyChanged(string str)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(str));
        }
    }

    public DataView Categories
    {
        get { return categories; }
        set 
        {
            if (value != categories)
            {
                categorias = value;
                NotifyPropertyChanged("Categories");
            }
        }
    }
}

My Class with INotifyCollectionChanged:

public class DataTest : INotifyCollectionChanged
{
    public event NotifyCollectionChangedEventHandler CollectionChanged;

    private void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        if (CollectionChanged != null)
        {
            CollectionChanged(this, e);
        }
    }

    public DataView Categories
    {
        get { return categories; }
        set 
        {
            if (value != categories)
            {
                categories = value;
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, Categories));
            }
        }
    }
}

But why the PropertyChanged is always NULL??? I have to do something more, but i don't know what.

Thanks in advance!

Best Answer

the DataTest class needs to be the DataContext for the Binding. You can set this in code-behind (or a myriad of other ways, but for simplicity - just do it in code-behind)

public MainWindow() // constructor
{
    this.DataContext = new DataTest();
}

Then, with the 'Source' of the Binding set, you can specify the 'Path', so your xaml looks like this:

<ListBox ItemsSource="{Binding Categories}" />

Now, if the property 'Categories' is changed in code, the NotifyPropertyChanged code you have written will alert the Binding, which in turn will access the public getter for the property and refresh the view.

Related Topic