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

发布于 2024-12-06 19:50:25 字数 2368 浏览 1 评论 0原文

我将 WPF DataGrid 放置在 Window 上。我有一个按钮,它执行一些业务逻辑并填充网格。

当我从按钮按 TAB 时,我想将焦点设置在 DataGrid(最好是 DataGrid 中的第一行)上。 我已经设置了 >TabIndex 但不知怎的,焦点并不集中在 DataGrid 上。

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

当我在 Button 的 LostFocus 事件上添加以下代码时,它突出显示第一行。但是当我使用“向下”箭头键选择网格中的下一行时;它不会转到下一行,而是首先将焦点设置在“搜索”按钮上,然后下次转到第二行!

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

如何将焦点设置在 DataGrid(或其第一行)上?为什么 TabIndex 在这里不起作用?

I have WPF DataGrid placed on the Window. 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. I have set the TabIndex but somehow the focus does not come on the DataGrid.

The relevant part of the form's XAML is here:

<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. 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)? Why TabIndex does not work here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

古镇旧梦 2024-12-13 19:50:25

如果控件层次结构中的 ButtonDataGrid 之间没有可聚焦的控件,您甚至不需要选项卡索引。你们有很多管理人员,一切对我来说似乎都有点复杂。我无法在该代码中发现任何内容(当然也许其他人可以),我的建议是您尝试简化代码,直到它再次正常工作为止。例如,这段代码的制表符应该可以工作:

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

另外,为什么制表符索引增量很大?如果您要使用 1301 & 1302 之间不能有任何值,因此它应该自动成为更安全的代码。

You do not even need a tab-index if there are no focusable controls between the Button and DataGrid in the control hierarchy. 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. e.g. 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.

花想c 2024-12-13 19:50:25

在 xaml 的 datagrid 中添加以下代码

SelectedIndex="0" Loaded="DataGrid_Loaded"

在 .cs 文件中添加以下代码

private void DataGrid_Loaded(object sender, RoutedEventArgs e)

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

Add following code in datagrid of xaml

SelectedIndex="0" Loaded="DataGrid_Loaded"

add below code in .cs file

private void DataGrid_Loaded(object sender, RoutedEventArgs e)

    {
        DataGrid dataGrid = sender as DataGrid;
        dataGrid.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    }
素手挽清风 2024-12-13 19:50:25
DataGrid1.SelectedIndex = 0;

DataGrid1.Focus();

您可以将任何您想要的整数放入选定的索引中。

DataGrid1.SelectedIndex = 0;

DataGrid1.Focus();

You can put any integer you want to the selectedindex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文