在 WPF 中使用 INotifyPropertyChanged
您好,我正在尝试使用 NotifyPropertyChanged 来更新我绑定属性的所有位置。对于我搜索过的内容,INotifyPropertyChanged 表明了这种情况。
所以我需要帮助,因为我不明白我在这里出了什么问题。我真的不知道如何处理 PropertyChange 事件。我的问题是,他什么时候改变?我对他还有什么好说的?
Datagrid:
<ListView Name="listView" ItemsSource="{Binding 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");
}
}
}
}
带有 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???我必须做更多的事情,但我不知道做什么。
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DataTest
类需要是绑定的DataContext
。您可以在代码隐藏中设置它(或多种其他方式,但为了简单起见 - 只需在代码隐藏中进行)然后,使用绑定集的“源”,您可以指定“路径”,这样您的xaml 看起来像这样:
现在,如果属性“Categories”在代码中发生更改,您编写的
NotifyPropertyChanged
代码将向 Binding 发出警报,Binding 反过来将访问公共 getter属性并刷新视图。the
DataTest
class needs to be theDataContext
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)Then, with the 'Source' of the Binding set, you can specify the 'Path', so your xaml looks like this:
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.获取处理程序可防止在检查 null 后事件处理程序变为 null,并且在没有事件处理程序的情况下检查 null 将防止您收到 null 异常。
PropertyChanged
为 null 的原因是没有附加任何事件处理程序。没有附加处理程序的原因是您没有将对象绑定到任何东西(这将负责添加处理程序)或者您没有向它添加处理程序(如果您出于其他原因想观察它) 。创建对象后,您需要将其绑定到某处。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.你正在做你必须做的一切。事件只是一种特殊的委托。您声明它,调用它,客户订阅它。这就是全部内容了。
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.