public static object GetElementFromPoint(System.Windows.Controls.ItemsControl itemsControl, Point point) { UIElement element = itemsControl.InputHitTest(point) as UIElement; while (element != null) { if (element == itemsControl) return null; object item = itemsControl.ItemContainerGenerator.ItemFromContainer(element); if (!item.Equals(DependencyProperty.UnsetValue)) return item; element = (UIElement)System.Windows.Media.VisualTreeHelper.GetParent(element); } return null; } private void Datagrid_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e) { var item = GetElementFromPoint((ItemsControl)sender, e.GetPosition((ItemsControl)sender)); }
或者绑定的是DataTable
private void DataGrid1_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) { //var s = this.dataGrid1.SelectedItem; DataRow row = (this.dataGrid1.SelectedItem as DataRowView).Row; if (row != null) { UInt64 ukey = Convert.ToUInt64(row["ukey"]); byte[] content = SqlExManager.GetDpuProtectContent(ukey, _dpu.ThisInpC.Sqlmodel); if (content != null) { ProtectTemapDll.ProtectTempClass.OpenProtecTemp(content, false); } else { MessageBox.Show("查询不到数据"); } } } target = VisualTreeHelper.GetParent(target); } }