I have a ListView containing several GridViewColumns. Several of the columns are checkboxes. Each column header consists of a checkbox as well, with the intention of checking/unchecking all the checkboxes in that column. Each row’s checkboxes are bound to properties in my view model. I’ve seen several postings where the scenario is a single column of checkboxes, but none of those solutions will work for me, as I have 4 columns of checkboxes. I also need to persist the state of the selections from one visit to the next (all, some or none of the checkboxes in a column could be checked).
Here’s an abbreviated version of the XAML for my ListView:
<ListView ItemsSource="{Binding AveragingParameters}">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn Header="Parameter">
<GridViewColumn.CellTemplate>
<DataTemplate>
<DockPanel>
<TextBlock Text="{Binding Name}" />
</DockPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<CheckBox x:Name="chkAvg" IsChecked="{Binding CalcAverage}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
<Grid>
<CheckBox x:Name="chkAvgSelectAll" Content="Avg" ToolTip="Select All" />
</Grid>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<CheckBox x:Name="chkMin" IsChecked="{Binding CalMin}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
<Grid>
<CheckBox x:Name="chkMinSelectAll" Content="Min" ToolTip="Select All" />
</Grid>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I’ve tried using Commands and Command Parameters, PropertyChanged events on the Checked property, and handling the Checked event in my code behind, but nothing gives me enough information to know what to change in the collection I’m bound to.
I suppose I could create a separate event handler for each, but I’d like to be able to handle this in a single event handler if possible, because eventually I will be creating a custom control that is a bit more malluable in terms of the columns displayed.
Thanks in advance
I’ve solved my issue using a Command along with a Tag containing the name of the model property I want set and a CommandParameter set to a multi-binding that is bound to the Tag and the Checkbox’s IsSelected property. The code follows:
My View’s Xaml Resources:
My View’s Xaml:
My MultiValueConverter:
The result object I’m using in the converter:
The DelegateCommand (I’m using PRISM) and handler in my View Model
I hope I’m not abusing StackOverflow providing the solution I came up with. I wasn’t sure of the etiquette here.