WPF DataGrid以编程方式转到第一行

jgwigjjp  于 2023-10-22  发布在  其他
关注(0)|答案(2)|浏览(102)

这是我的代码
版本1

public void scrollIntoGrid(DataGrid grid)
        {
            object item = grid.Items[0];
            grid.SelectedItem = item;
            grid.ScrollIntoView(item);
            grid.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
            grid.SelectedIndex = 0;
            //DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);         
            //row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Left));
            //row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
        }

版本2

public void scrollIntoGrid(DataGrid grid)
        {
            object item = grid.Items[0];
            grid.SelectedItem = item;
            grid.ScrollIntoView(item);
            //grid.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
            //grid.SelectedIndex = 0;
            DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
            row.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
            //row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
        }

所有的迭代都转到网格中的第二行而不是第一行。

jtjikinw

jtjikinw1#

下面的代码对我有用。单击按钮时,它选择第一行。如果这就是你对go to first row的意思。
如果您对第一行的解释是标题行,那么这将不起作用。你的scroolIntoGrid函数可能看起来像这样:

public void scrollIntoGrid(DataGrid grid)
    {

        if(grid != null && grid.HasItems)
        {
            grid.SelectedIndex = 0;
            grid.Focus();
        }
       
    }

完整代码在这里:
文件背后的代码:

public class Person
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    

}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var listOfPersons = new List<Person> { { 
                new Person { Firstname="Hello",Lastname="World"} },
            new Person {Firstname="World",Lastname="Hello"} };
        mygrid.DataContext = listOfPersons;
        
        
    }

    private void btnSearch_Click(object sender, RoutedEventArgs e)
    {
        scrollIntoGrid(this.mygrid);
    }


    public void scrollIntoGrid(DataGrid grid)
    {

        if(grid != null && grid.HasItems)
        {
            grid.SelectedIndex = 0;
            grid.Focus();
        }
       
    }

  
}

}
XAML文件:

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid >
    <StackPanel Orientation="Vertical" Grid.Column="2" Margin="20,10,10,10">
        <Button Content="Search"
            Height="25" HorizontalAlignment="Right"  Margin="20,10,10,10"
            Name="btnSearch" VerticalAlignment="Top" Width="75" TabIndex="1300" Click="btnSearch_Click" />
    </StackPanel>
    <DataGrid x:Name="mygrid" AutoGenerateColumns="False" Grid.Column="1" Margin="20,160,10,10" 
              BorderThickness="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Focusable="True"
              ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" GridLinesVisibility="None"
              ItemsSource="{Binding}" CanUserSortColumns="False" CanUserResizeColumns="False" IsReadOnly="True" 
             >
        <DataGrid.Columns>
            <DataGridTextColumn Header="Shortname" Width="100" Binding="{Binding Firstname}" />
            <DataGridTextColumn Header="Internal Code" Width="100" Binding="{Binding Lastname}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

根据你的评论,你实际上想从一个文本框上的箭头向下键跳转,下面的变化做了我的伎俩。
添加了一个带有PreviewKeyDown事件的XAML文本框

<TextBox x:Name="searchBox" PreviewKeyDown="searchBox_KeyDown"></TextBox>

在代码后面:

private void searchBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Down) // The Arrow-Down key
        {
            scrollIntoGrid(mygrid);
            e.Handled = true;
            Keyboard.Focus(mygrid);
        }
    }

ScrollIntoGrid保持与上面描述的相同。

hyrbngr7

hyrbngr72#

回到这一点后,一段时间,我发现工作的解决方案是在文本框中的KeyUp事件。

if (e.Key == Key.Down)
 {
  object item = dgProductSaleInvSm.Items[0];
  dgProductSaleInvSm.SelectedItem = item;
  dgProductSaleInvSm.ScrollIntoView(item);
  DataGridRow row = (DataGridRow)dgProductSaleInvSm.ItemContainerGenerator.ContainerFromIndex(0);
 row.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
 }

相关问题