wpf datagrid enter to edit

2019-07-20 08:44发布

问题:

I hope this is a simple one. I have a datagrid in a wpf application. one of the columns is a checkbox column. the user can only check the checkbox if the cell is already selected. so in effect to check any box the user has to double click, once to select, then once more to check the box. I want the user to be able to just check the box right a way with a single click. I couldn't find any obvious properties to make this happen. what's the best way to go about doing this?

回答1:

For other answers see this SO post but the answer I liked was way at the bottom so I'll repeat it here with some more detail.

That answer was: don't use a DataGridCheckBoxColumn. It's almost no more work to just put a Checkbox in a DataGridTemplate Column. When you do this it responds to a single click exactly like you want. In the example below IsSelected is just a custom bool property on my ViewModel, everything else is pure xaml.

<DataGridTemplateColumn>
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <CheckBox  IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}" />
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>