private static void OnAnyGotFocus(object sender, RoutedEventArgs e) { DataGridCell cell = DataGridHelper.FindVisualParent <DataGridCell>(e.OriginalSource as UIElement); if (cell != null && cell == sender) { DataGrid owner = cell.DataGridOwner; if (owner != null) { owner.FocusedCell = cell; } } }
private static void OnAnyLostFocus(object sender, RoutedEventArgs e) { // Get the ancestor cell of old focused element. // Set DataGrid.FocusedCell to null, if the cell doesn't // have keyboard focus. DataGridCell cell = DataGridHelper.FindVisualParent <DataGridCell>(e.OriginalSource as UIElement); if (cell != null && cell == sender) { DataGrid owner = cell.DataGridOwner; if (owner != null && !cell.IsKeyboardFocusWithin && owner.FocusedCell == cell) { owner.FocusedCell = null; } } }
/// <summary> /// Searchs up the visual parent chain from the given element until /// a DataGridRow element is found. /// </summary> /// <param name="element">The descendent of a DataGridRow.</param> /// <returns> /// The first ancestor DataGridRow of the element parameter. /// Returns null of none is found. /// </returns> public static DataGridRow GetRowContainingElement(FrameworkElement element) { return(DataGridHelper.FindVisualParent <DataGridRow>(element)); }