C# WPF Handling double click events from multiple, dynamically generate datagrids through a single handler
In my current implementation, I spawn tabs and grids dynamically.
Basically, a new grid needs to be created by a double click on a any row of a previous grid and use the row data for other provessing.
this.AddHandler(DataGrid.MouseDoubleClickEvent, new RoutedEventHandler 开发者_高级运维 (Generic_DoubleClick));
This handles for any double click even outside the grid and not specifically for the grid.
I need to find a handler which can return the row values specifically to that grid. Please suggest a workaround or a easier way of doing this.
Thanks.
Handle the double click routed event from datagrid row of the datagrid.
<tk:DataGrid>
<tk:DataGrid.Resources>
<Style TargetType="{x:Type tk:DataGridRow}">
<EventSetter Event="MouseDoubleClick"
Handler="DataGridRow_MouseDoubleClick"/>
</Style>
</tk:DataGrid.Resources>
<tk:DataGrid.ItemsSource>
<x:Array Type="{x:Type TextBlock}">
<TextBlock Text="1" Tag="1.1"/>
<TextBlock Text="2" Tag="1.2"/>
<TextBlock Text="3" Tag="1.3"/>
<TextBlock Text="4" Tag="1.4"/>
</x:Array>
</tk:DataGrid.ItemsSource>
<tk:DataGrid.Columns>
<tk:DataGridTextColumn Header="Text" Binding="{Binding Text}"/>
<tk:DataGridTextColumn Header="Tag" Binding="{Binding Tag}"/>
</tk:DataGrid.Columns>
</tk:DataGrid>
In code behind
private void DataGridRow_MouseDoubleClick(
object sender, MouseButtonEventArgs e)
{
var dgRow = sender as Microsoft.Windows.Controls.DataGridRow;
var cellContentElement = e.OriginalSource as UIElement;
}
Bonus is cellContentElement
is the content element of the cell that was double clicked on the row ... e.g. in case of DataGridTextColumn
it will be TextBlock
in the cell.
精彩评论