WPF Toolkit Datagrid - how do you turn selection off?

后端 未结 2 1395
离开以前
离开以前 2020-12-31 10:07

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

相关标签:
2条回答
  • 2020-12-31 10:49

    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>
    
    0 讨论(0)
  • 2020-12-31 11:00

    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 ;).

    0 讨论(0)
提交回复
热议问题