以突出显示方式以编程方式选择WPF DataGrid行 - c#

我在WPF中用几行创建了一个数据网格。
我在wpf网格上创建了​​四个按钮,以便在各行之间导航:[<<]-[]-[>>]

我使用SelectedItem函数来设置行。
我的问题是,突出显示似乎不好(很慢)(很难解释)。

当我在各行之间使用键盘箭头(向上和向下)时,突出显示既快速又迅速。将我的代码隐藏在按钮后面,突出显示有点慢且奇怪。

这是我的代码

        private void Button_Click_Goto_Premier(object sender, RoutedEventArgs e)
    {
        myDataGridEvtCode.SelectedItem = myDataGridEvtCode.Items[0];
        myDataGridEvtCode.Focus();
    }

    private void Button_Click_Goto_Precedent(object sender, RoutedEventArgs e)
    {
        if (myDataGridEvtCode.SelectedIndex > 0)
        {
            myDataGridEvtCode.SelectedItem = myDataGridEvtCode.Items[myDataGridEvtCode.SelectedIndex - 1];
            myDataGridEvtCode.Focus();
        }
    }

    private void Button_Click_Goto_Suivant(object sender, RoutedEventArgs e)
    {
        if (myDataGridEvtCode.SelectedIndex < myDataGridEvtCode.Items.Count - 1)
        {
            myDataGridEvtCode.SelectedItem = myDataGridEvtCode.Items[myDataGridEvtCode.SelectedIndex + 1];
            myDataGridEvtCode.Focus();
        }
    }

    private void Button_Click_Goto_Dernier(object sender, RoutedEventArgs e)
    {
        myDataGridEvtCode.SelectedItem = myDataGridEvtCode.Items[myDataGridEvtCode.Items.Count-1];
        myDataGridEvtCode.Focus();
    }

有人对此有一些想法吗?

非常感谢我的朋友们:)

参考方案

我假设您使用System.Windows.Control.DataGrid。我没有尝试您的代码。这是一些代码,我只是将它们放到一个混合的WPF应用程序中。通过按下按钮进行选择就像使用鼠标/键盘手动选择行一样流畅。

XAML

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <DataGrid x:Name="MyGrid" Height="200"/>
        <Button Content="Previous" Click="Previous"/>
        <Button Content="Next" Click="Next"/>
    </StackPanel>
</Window>

后面的代码

namespace WpfApplication3
{
    public class Person
    {
        public Person(string firstName, string lastName)
        {
            FirstName = firstName;
            LastName = lastName;
        }

        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            var persons = new List<Person>
                {
                    new Person("Steve", "Jobs"),
                    new Person("Bill", "Gates"),
                    new Person("Dan", "Brown"),
                    new Person("Barack", "Obama")
                };

            MyGrid.ItemsSource = persons;
        }

        private void Next(object sender, RoutedEventArgs e)
        {
            MyGrid.Focus();

            int nextIndex = MyGrid.SelectedIndex + 1;
            if (nextIndex > MyGrid.Items.Count - 1) return;
            MyGrid.SelectedIndex = nextIndex;
        }

        private void Previous(object sender, RoutedEventArgs e)
        {
            MyGrid.Focus();

            int previousIndex = MyGrid.SelectedIndex - 1;
            if (previousIndex < 0) return;
            MyGrid.SelectedIndex = previousIndex;
        }
    }
}

使用指数可能是线索。虽然我还没有寻找证据。

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

改造正在返回一个空的响应主体 - java

我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…

每个文件合并后添加换行 - python

我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…

Json到php,json_decode返回NULL - php

我正在用PHP进行JSON解析器的一些API,用于存储有关遗产的信息。我在解析时遇到问题,因为它返回的是NULL值而不是数组或对象。简单的JSON代码可以很好地解析,但是可以这样:{"success":true,"totalCount":1,"data":[{"id":99694…

您如何在列表内部调用一个字符串位置? - python

我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…