如何为 bool[] 中的值实现 INotifyPropertyChanged 功能?
我有一个大小为 4 的布尔数组,我想将每个单元格绑定到不同的控件。 该布尔数组代表 4 种状态(false = 失败,true = 成功)。 这个 bool 数组是一个类的属性:
class foo : INotifyPropertyChanged {
...
private bool[] _Statuses;
public bool[] Statuses
{
get {return Statuses;}
set {
Statuses = value;
OnPropertyChanged("Statuses");
}
}
在 XAML 中有 4 个控件,每个控件都绑定到数组的一个单元格:
... Text="{Binding Path=Statuses[0]}" ...
... Text="{Binding Path=Statuses[1]}" ...
... Text="{Binding Path=Statuses[2]}" ...
... Text="{Binding Path=Statuses[3]}" ...
问题是仅当我更改数组本身时才会引发通知事件,而当我更改数组本身时则不会引发通知事件更改数组中的一个值,即下一个代码行引发事件:
Statuses = new bool[4];
但下一行不会引发事件:
Statuses [0] = true;
每次更改一个单元格时如何引发事件?
I have a bool array of size 4 and I want to bind each cell to a different control.
This bool array represents 4 statuses (false = failure, true = success).
This bool array is a propery with a class:
class foo : INotifyPropertyChanged {
...
private bool[] _Statuses;
public bool[] Statuses
{
get {return Statuses;}
set {
Statuses = value;
OnPropertyChanged("Statuses");
}
}
In XAML there are 4 controls, each one bound to one cell of the array:
... Text="{Binding Path=Statuses[0]}" ...
... Text="{Binding Path=Statuses[1]}" ...
... Text="{Binding Path=Statuses[2]}" ...
... Text="{Binding Path=Statuses[3]}" ...
The problem is that the notify event is raised only when I change the array itself and isn't raised when I change one value within the array, i.e, next code line raises the event:
Statuses = new bool[4];
but next line does not raises the event:
Statuses [0] = true;
How can I raise the event each time one cell is changed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将状态公开为索引器,然后引发属性更改事件以指示索引器已更改。
请参阅此博客文章:
http ://10rem.net/blog/2010/03/08/wpf---silverlight-quick-tip-inotifypropertychanged-for-indexer
You need to expose your statuses as an indexer, then raise a property change event that indicates that the indexer has changed.
See this blog post:
http://10rem.net/blog/2010/03/08/wpf---silverlight-quick-tip-inotifypropertychanged-for-indexer
它不会引发事件,因为
Array
本身没有实现INotifyPropertyChanged
。您可以使用与原始数组不同的容器(任何实现INotifyCollectionChanged
的容器(如ObservableCollection
应该这样做))或者您必须调用RaisePropertyChanged("Statuses ")
每次更新 Statuses 数组时,或者如另一个答案中提到的,使用一个实现包含 4 个属性的INotifyPropertyChanged
的类。It doesn't raise the event becuase
Array
itself doesn't implementINotifyPropertyChanged
. You can either use a different container than the primitive array (anything that implementsINotifyCollectionChanged
likedObservableCollection<T>
should do) OR you have to callRaisePropertyChanged("Statuses")
each time you update the Statuses array OR, as metioned in another answer, use one class that implementINotifyPropertyChanged
that contains 4 properties.使用
数组
时无法执行此操作。更改Array
上任何索引处的值不会引发UI
所需的更改通知。您可以使用具有四个属性并实现 INotifyPropertyChanged 接口的类吗?
You cannot do it while using an
Array
. Changing a value at any index on anArray
does not raise change notification required by theUI
.Can you use a class with four properties that implements
INotifyPropertyChanged
interface instead?