wpf datagrid 行双击事件

时间:2021-01-17 19:39:07

Xaml:

<DataGrid ItemsSource="{Binding SessionList}"
Grid.Row="2"
Grid.Column="0"
Grid.ColumnSpan="2"
AutoGenerateColumns="False"
ScrollViewer.CanContentScroll="False"
IsReadOnly="True"
HorizontalScrollBarVisibility="Auto"
SelectionMode="Single"
VerticalScrollBarVisibility="Auto"
MouseDoubleClick="datagrid_MouseDoubleClick"
CanUserAddRows="True"/>

cs代码:

private void datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
  DataGrid datagrid = sender as DataGrid;
  Point aP = e.GetPosition(datagrid);
  IInputElement obj = datagrid.InputHitTest(aP);
  DependencyObject target = obj as DependencyObject;


  while (target != null)
  {
    if (target is DataGridRow)
    {
      break;
    }
    target = VisualTreeHelper.GetParent(target);
  }
}