【问题标题】:How to set focus on the WPF DataGrid or its first row?如何将焦点设置在 WPF DataGrid 或其第一行上?
【发布时间】:2011-11-25 19:00:27
【问题描述】:

我将 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 在这里不起作用?

【问题讨论】:

  • 请通过 cmets 了解 H.B. 的答案。问题在 H.B. 之后得到解决。指出不需要 TabIndex。在 Button 控件的 StackPanel 中移动 DataGrid 解决了这个问题。

标签: c# wpf xaml wpf-controls wpfdatagrid


【解决方案1】:

如果控件层次结构中的ButtonDataGrid 之间没有可聚焦的控件,您甚至不需要选项卡索引。你有很多处理程序,对我来说,一切似乎都有些令人费解。我无法在该代码中发现任何内容(也许其他人当然可以),我的建议是您尝试简化您的代码,直到它再次正常工作,因为它应该默认。例如此代码的选项卡应该可以工作:

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

另外,为什么会有大的标签索引增量?如果您要使用 1301 和 1302 之间不能有任何值,所以它应该自动成为更安全的代码。

【讨论】:

  • 这是错字;我在按钮的 LostFocus 事件上添加了代码,而不是在 DataGrid 上。我现在已经更正了这个问题。
  • @CSharpLearner:好吧,无论如何你都不应该需要那个处理程序。
  • Button 和 DataGrid 之间没有控件。但是 Button 是 StackPanel 的一部分,而 DataGrid 是 StackPanel 之外的。顺便说一句,我删除了你提到的所有选项卡索引。 Tabbing 工作正常,但它总是跳过 DataGrid,焦点设置在 Button 的下一个控件(DataGrid 旁边)上。
  • @CSharpLearner:StackPanel 无关紧要,因为它不是输入元素。 (您可以将我的示例代码中的 Button 包装在一个中,它仍然可以工作。)只需尝试进一步简化您的代码以找出问题的根源。
  • 感谢您的时间和回答。它帮助了我。我删除了标签索引并将DataGrid 移动到StackPanel 中,它解决了我的问题!在DataGrid 内按标签将控制从一个单元格移动到另一个单元格,我现在想停止,但这不是这个问题的一部分。
【解决方案2】:

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

【讨论】:

    【解决方案3】:
    DataGrid1.SelectedIndex = 0;
    
    DataGrid1.Focus();
    

    您可以将任何您想要的整数放入 selectedindex。

    【讨论】:

      猜你喜欢
      • 2018-10-04
      • 2011-06-30
      • 2011-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-23
      • 2011-10-12
      相关资源
      最近更新 更多