WPF: Check/Uncheck all checkbox for checkboxes located in gridview cell template?
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));
}
}
Comments
-
Robin almost 2 years
I'm trying to create a check/uncheck all
CheckBox
for a number ofCheckBoxes
that are located inside the cell template of aGridViewColumn
. I added this column to aGridView
(along with other columns), set theGridView
to the view property of aListView
, and then databound theListView
to a collection of customDataObjects
. So, each row of theListView
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 theIsChecked
property of theCheckBoxes
, but I do not want to change the data object theListView
is bound to. My first attempt was to bind the ListView to aDictionary<DataObject,Boolean>
and then bind theIsChecked
property to theValue
of theDictionary
and the other columns toKey
.DataObjectProperty
. Then, I simply toggled theValues
of the Dictionary when then check/uncheck allCheckBox
was clicked. The binding to worked properly, but apparently dictionaries don't support change notification so theCheckBoxes
were never updated.Does anyone have any suggestions as to the best way to solve this problem?
-
Robin over 15 yearsExcellent 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; } ... }