I have a datagrid in WPF with DataGridTextColum and DataGridTemplateColum .
<DataGridTextColumn Width="4*" IsReadOnly="True" x:Name="dataGridColumnDescription" Header="Description" Binding="{Binding Description}"> </DataGridTextColumn> <DataGridTemplateColumn CellStyle="{StaticResource CellEditing}" IsReadOnly="False" Width="*" Header="Value" CellEditingTemplateSelector="{StaticResource myCellEditingTemplateSelectorValue}" CellTemplateSelector="{StaticResource myCellTemplateSelectorValue}"> </DataGridTemplateColumn>
CellTemplateSelectors return a DataTemplate with a TextBlock for the Celltemplate, respectively. TextBox for CellEditing!
<DataTemplate x:Key="dGridStringValueTemplate"> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Path=Value}"/> </DataTemplate> <DataTemplate x:Key="dGridStringValueTemplateEditing"> <TextBox TextAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" BorderThickness="1" Text="{Binding Path=Value, UpdateSourceTrigger=LostFocus}"/> </DataTemplate>
Now I want to automatically focus the TextBox when the DataGridCell gets focus. The user should be able to edit the contents of the TextBox without double-clicking on the cell.
I found this article:
DataGrid Tips and Tricks: One-Click Editing Where can I get the current DataGridCell, but how can I access the content to give the text block the ability to edit the content?
This is my style:
<Style x:Key="CellEditing" TargetType="{x:Type DataGridCell}"> <EventSetter Event="PreviewMouseLeftButtonDown" Handler="myDataGridMain_PreviewMouseLeftButtonDown"></EventSetter> </Style>
This is my event handler:
private void myDataGridMain_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DataGridCell cell = sender as DataGridCell; // cell ist not null DataGridTemplateColumn col = cell.Column as DataGridTemplateColumn; //col is not null DataTemplate template = col.CellTemplate; //this is null }
How can I get a text box with this event handler?
c # wpf xaml datagrid
nullxff
source share