Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGrid's CellEditingTemplate and focus in edit mode

Tags:

I am having an issue with WPFToolkit DataGrid when a column is customized supplying both CellTemplate and CellEditingTemplate. If you take a look below, you will see my editing template has a single CheckBox. All is fine in a functional sense but when F2 is hit to edit the cell, one must also hit TAB in order for the CheckBox to receive focus. Ideally, one would hit F2 and SPACE to toggle the value. Currently, one must hit F2, TAB, SPACE. I have tried setting TabIndex to no avail. I am running out of ideas.

<WPFToolkit:DataGridTemplateColumn Header="Turn"
                                   MinWidth="60">
    <WPFToolkit:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Height="16">
                <Image.Style>
                    <Style TargetType="{x:Type Image}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding CanTurn}" Value="True">
                                <Setter Property="Source" Value="/Images/16/Tick.png" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Image.Style>
            </Image>
        </DataTemplate>
    </WPFToolkit:DataGridTemplateColumn.CellTemplate>

    <WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding Path=CanTurn}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" />
        </DataTemplate>
    </WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
</WPFToolkit:DataGridTemplateColumn>
like image 834
wpfwannabe Avatar asked Oct 19 '10 13:10

wpfwannabe


2 Answers

Try this

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <CheckBox Name="checkbox" IsChecked="{Binding Path=CanTurn}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" />
        <DataTemplate.Triggers>
            <Trigger SourceName="checkbox" Property="IsVisible" Value="True">
                <Setter TargetName="checkbox" Property="FocusManager.FocusedElement" Value="{Binding ElementName=checkbox}" />
            </Trigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
like image 73
Fredrik Hedblad Avatar answered Sep 21 '22 20:09

Fredrik Hedblad


Or this...

<DataGridTemplateColumn Header="Long" IsReadOnly="False" Width="100">
  <DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
      <CheckBox FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}" IsChecked="{Binding Path=CanTurn}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" />
    </DataTemplate>
  </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
like image 14
SteffenSH Avatar answered Sep 21 '22 20:09

SteffenSH