mercredi 23 novembre 2016

How can I let my DataGrid rows be highlighted only if selected by CheckBox?

I have the following DataGrid:

<DataGrid x:Name="TheList" Grid.Row="1" ItemsSource="{Binding People}" SelectionMode="Extended" SelectionUnit="FullRow" AutoGenerateColumns="False" 
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Binding="{Binding IsSelected}">
            <DataGridCheckBoxColumn.HeaderTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Path=DataContext.AllSelected, RelativeSource={RelativeSource AncestorType=DataGrid}}"></CheckBox>
                </DataTemplate>
            </DataGridCheckBoxColumn.HeaderTemplate>
        </DataGridCheckBoxColumn>
        <DataGridTextColumn Binding="{Binding GivenName}" Header="Given Name" />
        ...
        <DataGridTextColumn Binding="{Binding BranchName}" Header="Branch" />
    </DataGrid.Columns>
</DataGrid>

I prefer using the CheckBox for selection because it gives - I think - clearer, more positive user feedback as to what is selected.

My problem now is that if I click on a row, that row is highlighted in blue, but in the item for that row, the IsSelected property is still false. It's fairly clear that it isn't selected, because the checkbox is unchecked.

The same if I click a checkbox, I must click twice, once to first select the row as an 'active row', and the second click to check the checkbox. I can then click the row again, and it is no longer highlighted, but it is clear that the row is selected due to the checkbox being checked.

This creates some kind of dissonance in my UX. I would prefer that clicking a row doesn't highlight it, but when I check its checkbox, it becomes highlighted.

Can I achieve this, and how? If not, in the worst case I would like to turn off the DataGrid's own row selection, and have no highlighted rows, only checked or unchecked checkboxes.




Aucun commentaire:

Enregistrer un commentaire