在WPF中使用INotifyPropertyChanged [英] Using INotifyPropertyChanged in WPF
问题描述
我正在尝试使用NotifyPropertyChanged来更新我绑定属性的所有位置.对于我搜索到的INotifyPropertyChanged,将显示这种情况.
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.
所以我需要帮助,因为我不明白自己在这里遇到的问题.而且我真的不知道该如何处理PropertyChange事件.我的问题是,他什么时候改变?我还有什么要和他在一起?
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}"/>
更改类别属性的示例:
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:
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?我必须做更多的事情,但我不知道该怎么办.
But why the PropertyChanged is always NULL??? I have to do something more, but i don't know what.
提前谢谢!
推荐答案
DataTest
类必须是绑定的DataContext
.您可以通过代码隐藏设置(或多种其他方式,但是为了简单起见,只需在代码隐藏设置即可)
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();
}
然后,在Binding设置为'Source'的情况下,您可以指定'Path',因此您的xaml如下所示:
Then, with the 'Source' of the Binding set, you can specify the 'Path', so your xaml looks like this:
<ListBox ItemsSource="{Binding Categories}" />
现在,如果代码中更改了类别"属性,则您编写的NotifyPropertyChanged
代码将提醒绑定,绑定将依次访问该属性的公共获取器并刷新视图.
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.
这篇关于在WPF中使用INotifyPropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!