繁体   English   中英

通过突出显示以编程方式选择WPF DataGrid行

[英]Select a wpf datagrid row programmaticaly with the highlighting

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM