DataGrid's CellEditingTemplate and focus in edit mode
17,798
Solution 1
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>
Solution 2
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>
Solution 3
if you want to set the focus on edit and select the text given by a Binding try this.
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Parameter0, Mode=TwoWay}" Loaded="TbLoaded" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
code behind:
private void TbLoaded(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
if (tb == null) return;
tb.SelectAll();
FocusManager.SetFocusedElement(this, tb);
}
Author by
wpfwannabe
Updated on June 03, 2022Comments
-
wpfwannabe almost 2 years
I am having an issue with WPFToolkit
DataGrid
when a column is customized supplying bothCellTemplate
andCellEditingTemplate
. If you take a look below, you will see my editing template has a singleCheckBox
. All is fine in a functional sense but when F2 is hit to edit the cell, one must also hit TAB in order for theCheckBox
to receive focus. Ideally, one would hit F2 and SPACE to toggle the value. Currently, one must hit F2, TAB, SPACE. I have tried settingTabIndex
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>
-
SteffenSH almost 13 yearsYou could possibly also move it to a style.. <Style x:Key="MyStyle" TargetType="CheckBox"> <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}"/> </Style>
-
knockando almost 13 yearsNice work. Although it didn't work initially because I was working with SelectedItem property on the DataGrid. Removed that and changed SelectionUnit="Cell" and this worked.
-
knockando almost 13 yearsNice work. Although it didn't work initially because I was working with SelectedItem property on the DataGrid. Removed that and changed SelectionUnit="Cell" and this worked.
-
Yola about 9 yearsWhat does this line actually do
"{Binding RelativeSource={RelativeSource Self}}"
? -
mack about 5 yearsThanks, this was helpful!