You can add the event setter to the cell style.
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
<EventSetter Event="MouseDoubleClick" Handler="DataGridCell_MouseDoubleClick"/>
</Style>
</DataGrid.CellStyle>
From there on, you could can use the sender
, which is the DataGridCell
to get the value.
private void DataGridCell_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var cell = (DataGridCell) sender;
var item = (YourDataItemType) cell.DataContext;
// The cell content depends on the column used.
var textBox = (TextBox) cell.Column.GetCellContent(item);
var value = textBox.Text;
// ...do something with the value
}
Note that this is only one approach that depends on the column used, these are alternatives:
A different way is to create your own data templates in a DataTemplateColumn
and use input bindings.
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type local:YourDataItemType}">
<TextBlock Text="{Binding id}">
<TextBlock.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding id}"/>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate DataType="{x:Type local:YourDataItemType}">
<TextBox Text="{Binding id}">
<TextBox.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding categName}"/>
</TextBox.InputBindings>
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
A benefit here is that you can use commands directly and pass the value as CommandParameter
.