提交数据后WPF Datagrid Cell Focus问题

时间:2013-07-30 17:34:32

标签: wpf focus datagridcell

我有datagrid,其中有SelectionUnit = FullRow。 当我在单元格上执行时,如果按Esc键,它将进入编辑模式而不进行更新。它出来了 从编辑模式,但它仍然有焦点(虚线)

但如果我更新数据然后按Escape键,它基本上使用(CellEditEnding事件)提交数据 然后当我尝试从代码中将焦点设置到该单元格时,它不起作用,整个数据网格失去焦点。

请参阅下面的代码。

//在更新数据后按Escape

时的CommitEdit代码
 void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        if (!_isManualEditCommit)
        {
            _isManualEditCommit = true;
            DataGrid grid = (DataGrid)sender;
            grid.CommitEdit(DataGridEditingUnit.Row, true);
            isManualEditCommit = false;
        }        
    } 


 void RhinoDataGrid_Loaded(object sender, RoutedEventArgs e)
  {

 RoutedEventHandler CellFocusedChangedHandler = new  RoutedEventHandler(FocusChangedHandler);
AddHandler(DataGridCell.GotKeyboardFocusEvent, CellFocusedChangedHandler);
}

  // Setting focus back to cell after updating data using Escape key

   private void FocusChangedHandler(object sender, RoutedEventArgs args)
    {
        if (args.RoutedEvent.RoutingStrategy == RoutingStrategy.Bubble)
        {
            FrameworkElement DataGridCellObj = args.OriginalSource as FrameworkElement;
           if (Keyboard.IsKeyDown(Key.Escape))
            {
                if (DataGridCellObj != null)
                {
                    DataGridCell dgCell = DataGridCellObj as DataGridCell;
                    if (dgCell != null && !dgCell.IsEditing && !dgCell.IsReadOnly)
                    {
                        if (!dgCell.IsFocused)
                        {
                            dgCell.Focus();
                        }
                        DataGridRow dr = VisualTreeHelpers.FindVisualParent<DataGridRow>(dgCell);
                        int rIndex = ((DataView)this.ItemsSource).Table.Rows.IndexOf(((DataRowView)(dr.Item)).Row);
                        DataGridCellInfo dgCellInfo = new DataGridCellInfo(this.Items[rIndex], dgCell.Column);
                        if (dgCellInfo != null)
                        {
                            FocusManager.SetIsFocusScope(dgCell, true);
                            FocusManager.SetFocusedElement(dgCell, dgCell);
                            dgCell.BorderThickness = new Thickness(7);
                        }
                    }
                }
            }
        }

enter image description here

请帮助 感谢

0 个答案:

没有答案