WPF update binding to element in an array

前端 未结 6 1466
野趣味
野趣味 2021-01-02 16:06

I\'ve searched my little heart out and its entirely possible that I\'m missing something critical and obvious.

I have a BitArray and a series of checkboxes that are

相关标签:
6条回答
  • 2021-01-02 16:21

    The thing that is missing is the notification that the value has changed. When you bind to standard .NET properties (so-called CLR Properties), you need to trigger an additional event to inform the control about the value change. Have a look at this SO Question. In addition MSDN might be helpful.

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

    0 讨论(0)
  • 2021-01-02 16:23

    Again, this TextBox gets the name from the binding just fine, but doesn't hit the setter if I change it.

    It doesn't need to call the setter: the binding doesn't replace the array, it simply replaces an element of the array. If you check the values in the array, you will see that they reflect the changes.

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

    However, arrays (and BitArray) don't implement INotifyPropertyChanged or INotifyCollectionChanged, so if there are other bindings to the values in the array, they won't be refreshed automatically. You will need a wrapper that implements these interfaces (ObservableCollection<T> for instance)

    0 讨论(0)
  • 2021-01-02 16:32

    This worked for me:

    NotifyPropertyChanged("")
    
    0 讨论(0)
  • 2021-01-02 16:43

    You don't end up in the setter because you don't change the value for NameArray, you change the value for a specific index in the array, e.g NameArray[1]. So the binding works but you won't end up in the setter.

    A better approach is to use an ObservableCollection and bind it to an ItemsControl

    0 讨论(0)
  • 2021-01-02 16:44

    I've never tried this approach before but I don't think this will work. Because the property you are waiting to see the setter fire is not the property bound. NameArray is not the same as NameArray[i].

    I would suggest looking into the ObservableCollection and templating to achieve multiple checkboxes. For example you could create a horizontal listbox of checkboxes that bind to an ObservableCollection.

    0 讨论(0)
  • 2021-01-02 16:44

    You will not be able to set individual array elements using element-index binding. You will need to split the collection up and set up 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 as per MSDN (http://msdn.microsoft.com/en-us/library/ms743695.aspx).

    0 讨论(0)
提交回复
热议问题