Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve INotifyPropertyChanged functionality for the values in a bool[]?

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?

like image 657
yomi Avatar asked Jan 05 '11 13:01

yomi


People also ask

How do you use INotifyPropertyChanged?

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

What is INotifyPropertyChanged in WPF?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .

What is INotifyPropertyChanged xamarin?

The PropertyChanged event notifies the UI that a property in the binding source (usually the ViewModel) has changed. It allows the UI to update accordingly. The interface exists for WPF, Silverlight, UWP, Uno Platform, and Xamarin.


1 Answers

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

like image 121
ColinE Avatar answered Oct 02 '22 08:10

ColinE