views:

5436

answers:

1

I'm trying to create a check/uncheck all CheckBox for a number of CheckBoxes that are located inside the cell template of a GridViewColumn. I added this column to a GridView (along with other columns), set the GridView to the view property of a ListView, and then databound the ListView to a collection of custom DataObjects. So, each row of the ListView has a column that contains a checkbox as well as columns bound to property paths of the bound object.

I would like to create the check/uncheck all CheckBox by binding the IsChecked property of the CheckBoxes, but I do not want to change the data object the ListView is bound to. My first attempt was to bind the ListView to a Dictionary<DataObject,Boolean> and then bind the IsChecked property to the Value of the Dictionary and the other columns to Key.DataObjectProperty. Then, I simply toggled the Values of the Dictionary when then check/uncheck all CheckBox was clicked. The binding to worked properly, but apparently dictionaries don't support change notification so the CheckBoxes were never updated.

Does anyone have any suggestions as to the best way to solve this problem?

A: 

The only way I can think is to wrap your DataObject and boolean inside a new class which implements INotofyPropertyChanged. say the new class is YourCollection. Bind an ObservableCollection< YourNewClass > instance to your ListView

   public class YourNewClass :INotifyPropertyChanged
{
    public YourDataObject Object { get; set; }

    private bool _isChecked;
    public bool IsChecked
    {
        get
        {
            return _isChecked;
        }
        set
        {
            _isChecked = value;
            OnPropertyChanged("IsChecked");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
Jobi Joy
Excellent idea, thank you. This would still work if YourNewClass was generic where the generic argument is the type of YourDataObject correct? That is: public class YourNewClass <T> :INotifyPropertyChanged { public T Object { get; set; } ... }
Robin
The generic class does work as expected.
Robin