C# 如何将焦点设置为WPF toolkit datagrid的特定单元格

C# 如何将焦点设置为WPF toolkit datagrid的特定单元格,c#,wpf,wpfdatagrid,C#,Wpf,Wpfdatagrid,我正在使用WPF工具包提供的DataGrid控件来显示产品列表及其OpenStock、描述等。在这个DataGrid中,我将OpenStock列设置为可编辑,其余的是不可编辑的。当我的这个窗口加载时,我想将键盘焦点设置为OpenStock列的第一个单元格,如果可能的话,设置为编辑模式。我搜索了2天,终于在这里发布了 任何帮助或代码示例都会有所帮助 <my:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" Margin=

我正在使用WPF工具包提供的DataGrid控件来显示产品列表及其OpenStock、描述等。在这个DataGrid中,我将OpenStock列设置为可编辑,其余的是不可编辑的。当我的这个窗口加载时,我想将键盘焦点设置为OpenStock列的第一个单元格,如果可能的话,设置为编辑模式。我搜索了2天,终于在这里发布了

任何帮助或代码示例都会有所帮助

<my:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" Margin="2,2,2,55" 
x:Name="dataGrid1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="White"    
AlternatingRowBackground="AliceBlue" AlternationCount="2" SelectionMode="Single" 
SelectionUnit="Cell" BorderThickness="0" IsTabStop="True">
        <my:DataGrid.Resources>
            <Style x:Key="errorStyle" TargetType="{x:Type TextBox}">
                <Setter Property="Padding" Value="-2"/>
                <Style.Triggers>
                    <Trigger Property="Validation.HasError" Value="True">
                        <Setter Property="Background" Value="Yellow"/>
                        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},
                                Path=(Validation.Errors)[0].ErrorContent}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </my:DataGrid.Resources>
        <my:DataGrid.Columns>
            <my:DataGridTextColumn Width="60" FocusManager.IsFocusScope="False" Binding="{Binding Path=pCode}" Header="Code" IsReadOnly="True" />
            <my:DataGridTextColumn Width="150" Binding="{Binding pName}" Header="Description"  IsReadOnly="True" />
            <my:DataGridTextColumn Width="120" Binding="{Binding CloseStock}" Header="Closing Stock"  IsReadOnly="True" />
            <my:DataGridTextColumn  Width="100" Binding="{Binding OpenStock, ValidatesOnExceptions=True}" Header="Open Stock"
                                   EditingElementStyle="{StaticResource errorStyle}"/>
            <my:DataGridTextColumn Width="150" Binding="{Binding YrlyOpenStock}" Header="Yearly Opening"  IsReadOnly="True" />

        </my:DataGrid.Columns>
    </my:DataGrid>       


非常感谢

您需要将当前单元格设置为要编辑的单元格,然后调用加载的处理程序:

dataGrid1.CurrentCell = new DataGridCellInfo(
    dataGrid1.Items[0], dataGrid1.Columns[3]);
dataGrid1.BeginEdit();

如果在XAML中为DataGridTextColumn指定名称,则可以使用该名称,而不是
Columns[3]

使用此代码将滚动视图移动到特定单元格:

dgv.ScrollIntoView(dgv.Items[row], dgv.Columns[col]);

使用下面的功能,它将工作

private void SetFocusOnGrid(DataGrid grid, int index)
{
    grid.ScrollIntoView(grid.Items.GetItemAt(index));
    grid.SelectionMode = DataGridSelectionMode.Single;
    grid.SelectionUnit = DataGridSelectionUnit.FullRow;
    grid.SelectedIndex = index;

    DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
    row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}  
这对我很有用:

DataGridCellInfo dataGridCellInfo = new DataGridCellInfo(dataGrid1.Items[sampleRowIndex], dataGrid1.Columns[sampleColumnIndex]);
dataGrid1.SelectedCells.Clear();
dataGrid1.SelectedCells.Add(dataGridCellInfo);
这将选择要放置焦点的单元格


DataGridCellInfo对象提供有关单元格以及与该单元格关联的数据项的信息。当DataGrid控件获取单元格时,例如在CurrentCell或SelectedCells属性中,使用它来代替对实际DataGridCell对象的引用。查看更多信息。

我在DataGridTemplateColumn的DataTemplate中有datagrid和文本框。 此外,我使用Enter键而不是Tab键来聚焦文本框

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);
    }
}
我结合以下代码解决焦点问题:

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));

回答得很好。我必须先将焦点设置在dataGrid“dataGrid1.focus();”上,但随后它起了作用。请注意,
dataGrid1.columns
集合中列的顺序不一定是屏幕上显示的顺序,请参见中的说明。如果确实需要按显示顺序进行索引,只需将
dataGrid1.Columns.OrderBy(c=>c.DisplayIndex.ToArray()
存储在局部变量中,然后索引到该数组中即可。