wpf datagrid 行双击事件

时间:2021-10-13 22:32:31

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);
  }
}