使用突出显示程序选择WPF数据表行

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

有人对此有一些想法吗?

非常感谢我的朋友们:)


我不能告诉你为什么这很慢。也许你的项目集合非常大,数据网格缺乏虚拟化。 你听说过LINQ吗?它可能会使你的代码更容易。要选择第一个项目,你可以写myDataGridEvtCode.SelectedItem = myDataGridEvtCode.Items.FirstOrDefault(),对于最后一个则是myDataGridEvtCode.SelectedItem = myDataGridEvtCode.Items.LastOrDefault()。如果集合为空,它不会像你当前的代码一样抛出异常,而只会返回null。还有.First().Last()可用,但这些会抛出异常。 - Michael Schnerring
我只有四行,每行有两列!我的集合很小!我认为这是一个代码问题。 - Walter Fabio Simoni
你使用标准的 DataGrid 吗?还是使用第三方的表格控件? - Michael Schnerring
是的,标准数据网格! - Walter Fabio Simoni
所以请看我的答案,希望这对你有用。 - Michael Schnerring
1个回答

7

我假设你使用的是 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;
        }
    }
}

使用索引可能是关键。虽然我还没有找到证据。


谢谢,我尝试了你的示例代码,看起来很好!我会在几个小时内尝试修改我的代码 :) 谢谢! - Walter Fabio Simoni

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接