Associating a WPF Update with an Item in an Array

I was looking for my little heart and it is possible that I missed something important and obvious.

I have a BitArray and a series of flags attached to elements in an array, for example:

<CheckBox IsChecked="{Binding Permissions[0]}" /> <CheckBox IsChecked="{Binding Permissions[1]}" /> ... <CheckBox IsChecked="{Binding Permissions[5]}" /> 

They correctly get their values โ€‹โ€‹from this property, but changing the checkboxes does not cause this configuration tool to start.

I tried a very simple example, when one text block is attached to an element of a string array.

 class TestArray { private string[] _nameArray = new string[3]; public TestArray() { _nameArray[1] = "test name"; } public string[] NameArray { get { return _nameArray; } set { _nameArray = value; } } } 

Here's the user interface element:

 <TextBox Text="{Binding NameArray[1], UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" /> 

Again, this TextBox gets the name from the binding just fine, but does not change the setter if I change it.

This may well be a question about heads and may arise due to a serious lack of understanding, so thanks for your patience!

+6
source share
6 answers

I have never tried this approach before, but I don't think it will work. Because the property that you expect to see setter fire is not the boundary of the properties. NameArray does not match NameArray [i].

I would suggest looking at ObservableCollection and templating to get some checkboxes. For example, you can create a horizontal list of flags tied to an ObservableCollection.

+2
source

Again, this TextBox gets the name from the binding just fine, but does not change the setter if I change it.

No need to call the setter: binding does not replace the array, it just replaces the array element. If you check the values โ€‹โ€‹in the array, you will see that they reflect the changes.

It also works great with BitArray (I just tried with both an array and BitArray ).

However, arrays (and BitArray) do not implement INotifyPropertyChanged or INotifyCollectionChanged , so if there are other bindings to the values โ€‹โ€‹in the array, they will not be updated automatically. You will need a wrapper that implements these interfaces (e.g. ObservableCollection<T> )

+2
source

You do not get into the setter, because you do not change the value for NameArray , you change the value for a specific index in the array, for example NameArray [1]. Thus, the binding works, but you do not get into the setter.

The best approach is to use an ObservableCollection and bind it to ItemsControl

+2
source

You cannot set individual elements of an array using element index binding. You need to split the collection and configure individual properties:

 class TestArray : INotifyPropertyChanged { private string[] _nameArray = new string[3]; public TestArray() { _nameArray[1] = "test name"; } public string Name { get { return _nameArray[0]; } set { _nameArray[0] = value; NotifyPropertyChanged("Name"); } } } 

You will need to use INotifyPropertyChanged according to MSDN (http://msdn.microsoft.com/en-us/library/ms743695.aspx).

+1
source

What is missing is a notification that the value has changed. When you bind to standard .NET properties (the so-called CLR properties), you need to raise an additional event to inform the control that the value has changed. Take a look at this SO question . In addition, MSDN may be useful.

I can also read some basic WPF concepts first. Books like WPF in action (slightly outdated) or WPF Unleashed might help.

0
source

This worked for me:

 NotifyPropertyChanged("") 
0
source

Source: https://habr.com/ru/post/895789/


All Articles