如何为 bool[] 中的值实现 INotifyPropertyChanged 功能?

发布于 2024-10-10 14:58:29 字数 787 浏览 0 评论 0原文

我有一个大小为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

合久必婚 2024-10-17 14:58:29

您需要将状态公开为索引器,然后引发属性更改事件以指示索引器已更改。

private bool[] _Statuses;

public bool this[int index]
{
    get { return _Statuses[index]; }
    set
    {
        _Statuses[index] = value;

        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(Binding.IndexerName));
    }
}

请参阅此博客文章:

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.

private bool[] _Statuses;

public bool this[int index]
{
    get { return _Statuses[index]; }
    set
    {
        _Statuses[index] = value;

        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(Binding.IndexerName));
    }
}

See this blog post:

http://10rem.net/blog/2010/03/08/wpf---silverlight-quick-tip-inotifypropertychanged-for-indexer

度的依靠╰つ 2024-10-17 14:58:29

它不会引发事件,因为 Array 本身没有实现 INotifyPropertyChanged。您可以使用与原始数组不同的容器(任何实现 INotifyCollectionChanged 的容器(如 ObservableCollection 应该这样做))或者您必须调用 RaisePropertyChanged("Statuses ") 每次更新 Statuses 数组时,或者如另一个答案中提到的,使用一个实现包含 4 个属性的 INotifyPropertyChanged 的类。

It doesn't raise the event becuase Array itself doesn't implement INotifyPropertyChanged. You can either use a different container than the primitive array (anything that implements INotifyCollectionChanged liked ObservableCollection<T> should do) OR you have to call RaisePropertyChanged("Statuses") each time you update the Statuses array OR, as metioned in another answer, use one class that implement INotifyPropertyChanged that contains 4 properties.

不离久伴 2024-10-17 14:58:29

使用数组时无法执行此操作。更改 Array 上任何索引处的值不会引发 UI 所需的更改通知。

您可以使用具有四个属性并实现 INotifyPropertyChanged 接口的类吗?

You cannot do it while using an Array. Changing a value at any index on an Array does not raise change notification required by the UI.

Can you use a class with four properties that implements INotifyPropertyChanged interface instead?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文