简体   繁体   English

如何将焦点放在WPF DataGrid或其第一行上?

[英]How to set focus on the WPF DataGrid or its first row?

I have WPF DataGrid placed on the Window . 我在Window上放置了WPF DataGrid I have a button which performs some business logic and populates the grid. 我有一个执行一些业务逻辑并填充网格的按钮。

I want to set focus on the DataGrid (preferably first row in the DataGrid) when i TAB from the button. 当我通过按钮TAB时,我想将焦点放在DataGrid (最好是DataGrid第一行)上。 I have set the TabIndex but somehow the focus does not come on the DataGrid. 我已经设置了TabIndex但不知何故焦点不在DataGrid上。

The relevant part of the form's XAML is here: 表单的XAML的相关部分在这里:

<StackPanel Orientation="Vertical" Grid.Column="2" Margin="20,10,10,10">
    <Button Content="Search"
                Height="25" HorizontalAlignment="Right" Margin="0,-25,0,0"
                Name="btnSearch" VerticalAlignment="Top" Width="75" **TabIndex="1300"** Click="btnSearch_Click" Keyboard.KeyDown="btnSearch_PreviewKeyDown" LostFocus="btnSearch_LostFocus"/>
</StackPanel>
<DataGrid AutoGenerateColumns="False" Grid.Column="1" Margin="20,160,10,10" Name="dataGridOrganisations" **TabIndex="1400"**
          BorderThickness="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Focusable="True"
                ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" GridLinesVisibility="None"
           ItemsSource="{Binding}" CanUserReorderColumns="False" CanUserResizeRows="False" IsReadOnly="True" SelectionChanged="dataGridOrganisations_SelectionChanged" Keyboard.PreviewKeyDown="dataGridOrganisations_PreviewKeyDown"  >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Shortname" Width="100" Binding="{Binding ShortName}" />
        <DataGridTextColumn Header="Internal Code" Width="100" Binding="{Binding LocalID}"/>
        <DataGridTextColumn Header="Name" Width="*" Binding="{Binding Name}"/>
    </DataGrid.Columns>
</DataGrid>

When I added following code on Button's LostFocus event, it highlights the first row. 当我在Button的LostFocus事件上添加以下代码时,它突出显示了第一行。 But when I use 'down' arrow key to select next row in the grid; 但是当我使用“向下”箭头键选择网格中的下一行时; instead of going to next row it first sets focus on the 'Search' button and next time goes to second row! 而不是转到下一行,它首先将焦点放在“搜索”按钮上,而下一次转到第二行!

if (this.dataGridOrganisations != null && this.dataGridOrganisations.HasItems)
{
    this.dataGridOrganisations.Focus();
    if (dataGridOrganisations.Items != null && dataGridOrganisations.Items.Count > 0)
    {
        DataGridRow firstRow = this.dataGridOrganisations.ItemContainerGenerator.ContainerFromItem(dataGridOrganisations.Items[0]) as DataGridRow;
        if (firstRow != null)
        {
            firstRow.IsSelected = true;
        }
    }
}

How to set focus on the DataGrid (or its first row)? 如何将焦点放在DataGrid(或其第一行)上? Why TabIndex does not work here? 为什么TabIndex在这里不起作用?

You do not even need a tab-index if there are no focusable controls between the Button and DataGrid in the control hierarchy. 如果控件层次结构中的ButtonDataGrid之间没有可聚焦的控件,则甚至不需要tab-index。 You have quite a few handlers and everything seems a bit convoluted to me. 您有很多处理程序,对我来说一切似乎有些令人费解。 I cannot spot anything in that code (maybe someone else can of course), my suggestion would be that you try to simplify your code until it works again as it should by default. 我无法在该代码中发现任何东西(当然也可以有人发现),我的建议是您尝试简化代码,直到它再次正常工作为止(默认情况下)。 eg this code's tabbing should work: 例如,此代码的制表符应该起作用:

  <StackPanel>
    <Button Content="Lorem Ipsum"/>
    <DataGrid ItemsSource="{Binding Source={StaticResource Items}}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}"/>
        </DataGrid.Columns>
    </DataGrid>
  </StackPanel>

Also, why the large tab-index delta? 另外,为什么制表索引较大? If you were to use 1301 & 1302 there could not be any value between so it should automatically be safer code. 如果要使用1301和1302,则两者之间不能有任何值,因此它应该自动是更安全的代码。

Add following code in datagrid of xaml 在xaml的datagrid中添加以下代码

SelectedIndex="0" Loaded="DataGrid_Loaded"

add below code in .cs file 在.cs文件中添加以下代码

private void DataGrid_Loaded(object sender, RoutedEventArgs e)

    {
        DataGrid dataGrid = sender as DataGrid;
        dataGrid.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    }
DataGrid1.SelectedIndex = 0;

DataGrid1.Focus();

You can put any integer you want to the selectedindex. 您可以将想要的任何整数放在selectedindex中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM