12

I have a datagrid in WPF that I am binding to an object.

I have a DataGridCheckBoxColumn on there which I want the users to be able to go through and tick the ones they want. Problem is they have to click twice, once for selection then again to check/uncheck. How on earth do you turn this off, I've been searching for way to long to find the answer to this. The datagrid has SelectionMode and SelectionUnit properties - neither of which accept 'none' or 'go away'

Any help is appreciated! My code is below for reference

<my:DataGrid Margin="15"  Name="dgPreview" 
        AutoGenerateColumns="False" CanUserSortColumns="True" 
             CanUserDeleteRows="True" 
             Background="White" 
             ColumnHeaderHeight="20" 
             VerticalScrollBarVisibility="Visible" 
             RowDetailsVisibilityMode="Visible" 
             >

    <my:DataGrid.Columns>
        <my:DataGridCheckBoxColumn  MinWidth="50" Width="Auto" Header="Include" Binding="{Binding Include}" />
        <my:DataGridTextColumn MinWidth="50"  Width="Auto" Header="Override #" Binding="{Binding OverrideNumber}" />
        <my:DataGridTextColumn MinWidth="220" Width="*" Header="Name" Binding="{Binding Name}" />
        <my:DataGridTextColumn MinWidth="50" Width="Auto" IsReadOnly="True"  Header="Preview" Binding="{Binding Preview}" />
    </my:DataGrid.Columns>
</my:DataGrid>
thehennyy
  • 4,020
  • 1
  • 22
  • 31
RodH257
  • 3,552
  • 5
  • 36
  • 46
  • Newer question on this topic, with many good answers: http://stackoverflow.com/questions/3833536/how-to-perform-single-click-checkbox-selection-in-wpf-datagrid/8333704#8333704 – surfen Dec 20 '11 at 01:00

2 Answers2

19

The first click puts the cell in edit mode then the second click allows you to modify the checkbox. You can change this behavior by using a DataGridTemplateColumn instead of a DataGridCheckBoxColumn. Replace your DataGridCheckBoxColumn with this:

<my:DataGridTemplateColumn MinWidth="50" Width="Auto" Header="Include" SortMemberPath="Include">
   <my:DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
         <CheckBox Style="{StaticResource DataGridCheckBoxStyle}" IsChecked="{Binding Path=Include}" />
      </DataTemplate>
   </my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>

DataGridCheckBoxStyle just makes the CheckBox look a little nicer in the DataGrid:

<Style x:Key="DataGridCheckBoxStyle" TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}">
   <Setter Property="VerticalAlignment" Value="Center" />
   <Setter Property="Margin" Value="8,0,3,0" />
</Style>
John Myczek
  • 12,076
  • 4
  • 31
  • 44
  • 2
    Doesn't work for me in WPF 4.0. Thanks for the style though. :) – bugfixr Feb 16 '12 at 14:08
  • Works for me in WPF 4.0. However, I get a warning while compiling and in the designer for the "BasedOn"-Tag in the style: `The Resource "{x:Type CheckBox}" was not found` I just removed it and it stil works. – OneWorld Jan 21 '13 at 08:39
  • Per prior comment remove Style reference. Should look like: – Stephen McCormick Oct 30 '15 at 04:32
1

First of, I know this is a pretty old question but I still thought I'd try and answer it.

I had the same problem a couple of days ago and came across a surprisingly short solution for it (see this blog). Basically, all you need to do is replace the DataGridCheckBoxColumn definition in your XAML with the following:

<DataGridTemplateColumn Header="MyCheckBoxColumnHeader">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{Binding Path=MyViewModelProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

The upside of this solution is obvious - it's XAML-only; thus it effectively refrains your from burdening your code-back with additional UI logic and helps you maintain your status in the eyes of MVVM zealots ;).

Priidu Neemre
  • 2,813
  • 2
  • 39
  • 40
  • It's the same answer as the one above. – BenjaminPaul Mar 19 '14 at 17:55
  • Thanks for this answer. The above didn't work for me in the sense that clicking a check box didn't actually change the underlying objects. I believe the fact that you added in the Mode=TwoWay fixed that issue. – muaddib Jul 07 '17 at 14:39