how to set focus to particular cell of WPF toolkit datagrid

后端 未结 5 1357
再見小時候
再見小時候 2020-12-29 08:58

I am using WPF toolkit provided DataGrid control to display product list along with its OpenStock, Description etc. In this DataGrid i have set OpenStock column to editable

5条回答
  •  旧巷少年郎
    2020-12-29 09:25

    I have datagrid with TextBox in DataTemplate of DataGridTemplateColumn. Also i am using Enter instead of Tab to focus TextBox

    private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            e.Handled = true;
            var TabKey = new KeyEventArgs(e.KeyboardDevice, e.InputSource, e.Timestamp, Key.Tab);
            TabKey.RoutedEvent = Keyboard.KeyDownEvent;
            InputManager.Current.ProcessInput(TabKey);
        }
    }
    

    I solve focus problem with combination of this code:

    dataGrid.Focus();
    //In case of more columns
    //dataGrid.CurrentCell = new DataGridCellInfo(dataGrid.Items[0], dataGrid.Columns[1]);
    dataGrid.BeginEdit();
    (Keyboard.FocusedElement as UIElement).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    

提交回复
热议问题