不是在 DataGrid 中添加双击事件,而是为 DataGridRow 和 DataGridColumnHeader 添加单独的事件。更新的 XAML:
<DataGrid ItemsSource="{Binding Projects}" SelectedItem="{Binding SelectedProject}" MouseDoubleClick="DataGrid_MouseDoubleClick">
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="DataGridRow_MouseDoubleClick" />
</Style>
</DataGrid.Resources>
<DataGrid.Resources>
<Style TargetType="DataGridColumnHeader">
<EventSetter Event="MouseDoubleClick" Handler="DataGridColumnHeader_MouseDoubleClick" />
</Style>
</DataGrid.Resources>
</DataGrid>
这是后面的代码。
private void DataGridRow_MouseDoubleClick(object sender, System.Windows.RoutedEventArgs e)
{
// This is when a row is double clicked.
}
private void DataGridColumnHeader_MouseDoubleClick(object sender, System.Windows.RoutedEventArgs e)
{
// This is when header is double clicked.
}