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>