<Window x:Class="WpfApp8.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp8" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <DataGrid Name="dgSimple" IsReadOnly="True" MouseDoubleClick="dgSimple_MouseDobleClick" CanUserAddRows="False"> </DataGrid> </Grid> </Window>
using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace WpfApp8 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); List<User> users = new List<User>(); users.Add(new User() { Id = 1, Name = "小明", Birthday = new DateTime(2019, 5, 10) }); users.Add(new User() { Id = 2, Name = "小王", Birthday = new DateTime(2019, 4, 10) }); dgSimple.ItemsSource = users; } private void dgSimple_MouseDobleClick(object sender, MouseButtonEventArgs e) { IInputElement element = e.MouseDevice.DirectlyOver; if (element != null && element is FrameworkElement) { if (((FrameworkElement)element).Parent is DataGridCell)// 是DataGridCell才响应双击 { MessageBox.Show("响应行双击"); } } } } public class User { public int Id { get; set; } public string Name { get; set; } public DateTime Birthday { get; set; } } }