通过突出显示选择一个wpf datagrid行programmaticaly

Wal*_*oni 2 c# wpf xaml datagrid

我在WPF中用几行创建了一个datagrid.我在我的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();
    }
Run Code Online (Sandbox Code Playgroud)

有人对此有一些想法吗?

非常感谢我的朋友:)

Mic*_*ing 5

我假设你使用System.Windows.Control.DataGrid.我没有尝试你的代码.这是一些代码,我只是在一个samlple 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>
Run Code Online (Sandbox Code Playgroud)

代码背后

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用指标可能是线索.虽然我还没有找到证据.