Using INotifyPropertyChanged in WPF

穿精又带淫゛_ 提交于 2019-12-22 10:25:24

问题


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!


回答1:


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.




回答2:


Getting the handler prevents the event handler from going to null after you check for null and checking for null will prevent you from getting a null exception if there are no event handlers.

The reason that your PropertyChanged is null is that there are no event handlers attached to it. The reason there are not handlers attached to it is you have not bound your object to anything (which will take care of adding a handler) or you haven't added a handler to it (if you wanted to observe it for some other reason). Once your object is created you need to bind it somewhere.




回答3:


You are doing all you have to do. An event is just a special kind of delegate. You declare it, you invoke it, clients subscribe to it. That's all there is to it.



来源:https://stackoverflow.com/questions/5785265/using-inotifypropertychanged-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!