How to update property of ObservableCollection element from change in WPF DataGrid?

I have a WPF DataGrid whose data source is ObservableCollection. It is configured as follows:

public class ItemDataCollection : ObservableCollection<ItemData>
{
}

public class ItemData : INotifyPropertyChanged
{
    private bool _selected = true;
    public bool Selected 
    { 
        get
        {
            return _selected;
        }
        set
        {
            if (value != _selected)
            {
                _selected = value;
                NotifyPropertyChanged("Selected");
            }
        }
    }
    }


    _itemDataCol = new ItemDataCollection();
        <... fill the _itemDataCol with data here ...>
    dataGrid1.ItemsSource = _itemDataCol;

      

When the collection is refreshed, dataGrid1.Items.Refresh () refreshes dataGrid1 nicely. However, when I change the "Selected" property of a row by checking or unchecking the box on the row that matches that property, the item in the collection is not updated. I looked at the CollectionChanged ObeservableCollection event but it doesn't seem to trigger triggerd. What wiring do I need to get dataGrid1 to update the collection.

Update

All I did was set the ItemSource property to the ObservableCollection and let the columns auto-generate. I have since changed the binding directly and found more details on the problem. When I just check the checkbox - no notification is triggered. However, if I hit after validating the field, then the collection is updated. Here is the binding:

<DataGridCheckBoxColumn Binding="{Binding Path=Selected, Mode=TwoWay}" Header="Selected"></DataGridCheckBoxColumn>

      

So my guess is the question is, how do I get an update with the need to hit after checking or unchecking?

Update # 2 (I can't answer as my rep is not high enough yet) Okay. I think I have a solution. If I enable "UpdateSourceTrigger = PropertyChanged" in the binding, everything seems to work.

<DataGridCheckBoxColumn Binding="{Binding Path=Selected, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="Selected"></DataGridCheckBoxColumn>

      

Please leave comments if there are any negative consequences that may be missing. Thanks for the help!

+3


source to share


4 answers


CollectionChanged is for insertion and deletion. NotifyPropertyChanged is for updating items. In your posted code, you are not actually implementing INotifyPropertyChanged.

   public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

      



And I think there is a cleaner binding to the public property where you return _itemDataCol

Otherwise, TwoWay celopez3 answer

+4


source


An ObservableCollection

does not listen for its events <<21>; use instead BindingList

.

In other words, [a ObservableCollection

] notifies only when items are added or removed, not when the values โ€‹โ€‹of its items change, even if those items are implemented by INotifyPropertyChanged. By comparison, the BindingList listens for INotifyPropertyChanged, and so if its elements change, the changes will be reflected in the grid. As a result, grouping, sorting, and statistical functions will be updated.



http://xceed.com/CS/blogs/dontpanic/archive/2009/04/01/i-notify-we-notify-we-all-wait-no-we-don-t.aspx

+2


source


I'm not sure if you've posted enough information in your post, but I'll try to help. One of my first questions is, how do you bind to a checkbox? It should be noted that if you want the checkbox to affect the observable collection, you must set "two way" binding to the item, by default the binding is "one way" and will not automatically modify the item in your collection. Another way to handle this would be to add an event into this checkbox, which would manually change the value in the observable collection when clicked.

0


source


The answer is that I need to set the UpdateSourceTrigger to PropertyChanged (see Blam's answer). It seems like there is no need to specify a two way binding. Here's the markup from the working code:

<DataGridCheckBoxColumn Binding="{Binding Path=Selected,
     UpdateSourceTrigger=PropertyChanged}" 
     Header="Selected"></DataGridCheckBoxColumn>

      

0


source







All Articles