在WPF中使用INotifyPropertyChanged

本文关键字:INotifyPropertyChanged WPF | 更新日期: 2023-09-27 17:52:50

嗨,我试图使用NotifyPropertyChanged来更新我绑定属性的所有地方。对于我所搜索的INotifyPropertyChanged是针对这种情况的。

所以我需要帮助,因为我不明白我在这里错了什么。我真的不知道如何处理PropertyChange事件。我的问题是,他什么时候会改变?我和他还有什么关系?

Datagrid:

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

改变Categories属性的例子:

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?
}

我的类:

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

但是为什么PropertyChanged总是NULL??我必须做点什么,但我不知道该做什么。

提前感谢!

在WPF中使用INotifyPropertyChanged

DataTest类需要为绑定的DataContext。您可以在代码隐藏中设置它(或者无数其他方法,但为了简单起见—就在代码隐藏中设置它)

public MainWindow() // constructor
{
    this.DataContext = new DataTest();
}
然后,使用绑定集的'Source',你可以指定'Path',所以你的xaml看起来像这样:
<ListBox ItemsSource="{Binding Categories}" />

现在,如果属性'Categories'在代码中改变了,你写的NotifyPropertyChanged代码将通知绑定,绑定将访问该属性的公共getter并刷新视图

获取处理程序可以防止事件处理程序在检查为空后变为空,如果没有事件处理程序,检查为空将防止您获得空异常。

您的PropertyChanged为空的原因是没有事件处理程序附加到它。没有附加处理程序的原因是您没有将对象绑定到任何东西(这将负责添加处理程序),或者您没有为它添加处理程序(如果您出于其他原因想要观察它)。一旦你的对象被创建,你需要把它绑定到某个地方。

你在做你该做的。事件只是一种特殊的委托。你声明它,你调用它,客户订阅它。