Suppose I have a DataGrid. Suppose all the columns are TemplateColumns. When any cell gets focus I want it to go in EditMode.
What I have tried so far:
I have created a style for DataGridCell as follows:
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="GotFocus" Handler="DataGridCell_GotFocus" />
</Style>
In the Code-Behind of the Window :
private void DataGridCell_GotFocus(object sender, RoutedEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
{
cell.IsEditing = true;
}
}
Problems in above try :
I have to single click the cell to bring it in edit mode.
On focus, cell goes into edit mode but the textBox doesn't have keyboard focus so next tab press will be eaten up by textBox and will get the focus.
As you mentioned you have to press Tab twice to move focus to next cell. What you can do is put focus on TextBox on loaded event:
XAML for dummy element:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Name}" Loaded="TextBox_Loaded"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
Code behind:
private void TextBox_Loaded(object sender, RoutedEventArgs e)
{
(sender as TextBox).Focus();
}
Of course you can move the handler to some base style for all your TextBoxes in dataGrid so you don't have to hook the handler for all cells.
In case you don't want to have handler, you can also do that using interactivity
triggers like defined here:
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Name}">
<Interactivity:Interaction.Triggers>
<Interactivity:EventTrigger EventName="Loaded">
<local:TakeFocusAction />
</Interactivity:EventTrigger>
</Interactivity:Interaction.Triggers>
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
and behavior trigger action:
public class TakeFocusAction : TriggerAction<UIElement>
{
protected override void Invoke(object parameter)
{
AssociatedObject.Focus();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With