如何将焦点设置为WPF工具包datagrid的特定单元格

时间:2022-01-01 22:19:50

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 and rest are non-editable. What i want now when my this windows loads, I want to set keyboard focus to first cell of OpenStock column and if possible in edit mode. I searched this for 2 days and finally posting here.

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

any help or code sample would be helpful.

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

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

thanks alot........

非常感谢........

3 个解决方案

#1


36  

You need to set the current cell to the one you want edited and then call BeginEdit in your Loaded handler:

您需要将当前单元格设置为您想要编辑的单元格,然后在Loaded处理程序中调用BeginEdit:

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

If you give your DataGridTextColumn a name in XAML you can use that name rather than Columns[3].

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

#2


6  

Use this code to move scroll view to specific cell

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

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

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

#3


0  

Use below function, it will work.

使用下面的功能,它会工作。

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

#1


36  

You need to set the current cell to the one you want edited and then call BeginEdit in your Loaded handler:

您需要将当前单元格设置为您想要编辑的单元格,然后在Loaded处理程序中调用BeginEdit:

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

If you give your DataGridTextColumn a name in XAML you can use that name rather than Columns[3].

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

#2


6  

Use this code to move scroll view to specific cell

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

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

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

#3


0  

Use below function, it will work.

使用下面的功能,它会工作。

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