如何使用INotifyPropertyChanged实现DataTable属性 [英] How to implement DataTable property with INotifyPropertyChanged
问题描述
public DataTable Test
{
get {return this.testTable; }
set
{
...
...
base.OnPropertyChanged(Test);
}
}
public void X()
{
this.Test.Add(...); //我想这行将首先调用getter(this.Test = get Test),然后它将调用add letter,这意味着setter范围将永远不会触发。
base.OnPropertyChanged(Test); //我的解决方案在这里:)但我希望它有更好的方法。
}
是否有另一种解决方案?
有两种方式可以更改表数据:可以从集合中添加/删除元素,或者元素中的某些属性可能会更改。
第一种情况很容易处理:使您的收藏品成为 ObservableCollection< T>
。在表上调用 .Add(T item)
或 .Remove(item)
将触发更改通知到查看为您(并且表将相应更新)
第二种情况是您需要您的T对象来实现INotifyPropertyChanged ...
最终您的代码应该如下所示:
public class MyViewModel
{
public ObservableCollection< MyObject> MyData {get;组;
public class MyObject:INotifyPropertyChanged
{
public MyObject()
{
}
私人字符串_status;
public string Status
{
get {return _status;
set
{
if(_status!= value)
{
_status = value;
RaisePropertyChanged(Status); //传递更改的属性的名称
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if(handler!= null)
{
var e = new PropertyChangedEventArgs(propertyName);
处理程序(这个,e);
}
}
}
现在设置你的查看为您的ViewModel的一个实例,并绑定到集合,如:
< tk:DataGrid
ItemsSource ={Binding Path = MyData}
... />
希望这有帮助:)
Ian
I have created WPF MVVM application, and set WPFToolkit DataGrid binding to DataTable so I want to know how to implement DataTable property to notify changed. Currently my code is like below.
public DataTable Test
{
get { return this.testTable; }
set
{
...
...
base.OnPropertyChanged("Test");
}
}
public void X()
{
this.Test.Add(...); // I suppose this line will call to getter first (this.Test = get Test) and then it will call add letter, this mean that setter scope will never fire.
base.OnPropertyChanged("Test"); // my solution is here :) but I hope it has better ways.
}
Is it has another solution for this problem?
There are 2 ways your Table data could change: Either an element could be added/removed from the collection, or some properties from within an element could change.
The first scenario is easy to handle: make your collection an ObservableCollection<T>
. Invoking .Add(T item)
or .Remove(item)
on your table will fire a change notification through to the View for you (and the table will update accordingly)
The second scenario is where you need your T object to implement INotifyPropertyChanged...
Ultimately your code should look something like this:
public class MyViewModel
{
public ObservableCollection<MyObject> MyData { get; set; }
}
public class MyObject : INotifyPropertyChanged
{
public MyObject()
{
}
private string _status;
public string Status
{
get { return _status; }
set
{
if (_status != value)
{
_status = value;
RaisePropertyChanged("Status"); // Pass the name of the changed Property here
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
}
Now set the datacontext of your View to be an instance of your ViewModel, and bind to the collection, like:
<tk:DataGrid
ItemsSource="{Binding Path=MyData}"
... />
Hope this helps :) Ian
这篇关于如何使用INotifyPropertyChanged实现DataTable属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!