Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 选择带有高亮显示的wpf datagrid行ProgramMaticali_C#_Wpf_Xaml_Datagrid - Fatal编程技术网

C# 选择带有高亮显示的wpf datagrid行ProgramMaticali

C# 选择带有高亮显示的wpf datagrid行ProgramMaticali,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我在WPF中创建了一个包含几行的datagrid。 为了在行之间导航,我在wpf网格上创建了四个按钮:[] 我使用SelectedItem函数来设置行。 我的问题是突出显示看起来不好(慢)(有点难以解释) 当我使用键盘箭头(向上和向下)在两行之间切换时,高亮显示是快速和立即的。在我的按钮后面的代码中,高亮显示有点慢和奇怪 这是我的代码 private void Button_Click_Goto_Premier(object sender, RoutedEventArgs e)

我在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();
    }
private void按钮\u单击\u转到\u Premier(对象发送方,路由目标)
{
myDataGridEvtCode.SelectedItem=myDataGridEvtCode.Items[0];
myDataGridEvtCode.Focus();
}
私有无效按钮\u单击\u转到\u前置(对象发送方,路由目标)
{
如果(myDataGridEvtCode.SelectedIndex>0)
{
myDataGridEvtCode.SelectedItem=myDataGridEvtCode.Items[myDataGridEvtCode.SelectedIndex-1];
myDataGridEvtCode.Focus();
}
}
私有无效按钮\u单击\u转到\u suvant(对象发送方,路由目标)
{
if(myDataGridEvtCode.SelectedIndex
有人对此有什么想法吗


非常感谢我的朋友:)

我想您使用的是
System.Windows.Control.DataGrid
。我没有尝试你的代码。这里有一些代码,我刚刚在一个samlple WPF应用程序中拼凑出来。通过按下按钮进行选择与使用鼠标/键盘手动选择行一样流畅

XAML

代码隐藏
命名空间WpfApplication3
{
公共阶层人士
{
公众人物(字符串名、字符串名)
{
名字=名字;
LastName=LastName;
}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
}
公共部分类主窗口
{
公共主窗口()
{
初始化组件();
var persons=新列表
{
新人(“史蒂夫”、“乔布斯”),
新人(“比尔”、“盖茨”),
新人(“丹”、“布朗”),
新人(“巴拉克”、“奥巴马”)
};
MyGrid.ItemsSource=个人;
}
private void Next(对象发送方,RoutedEventArgs e)
{
MyGrid.Focus();
int nextIndex=MyGrid.SelectedIndex+1;
如果(nextIndex>MyGrid.Items.Count-1)返回;
MyGrid.SelectedIndex=nextIndex;
}
private void Previous(对象发送方,RoutedEventArgs e)
{
MyGrid.Focus();
int previousIndex=MyGrid.SelectedIndex-1;
如果(先前指数<0)返回;
MyGrid.SelectedIndex=以前的索引;
}
}
}

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

我无法告诉你为什么这样做很慢。可能您的项目集合非常大,数据网格缺乏虚拟化。你听说过林克吗?这可能会使您的代码更容易一些。用于选择要编写的第一个项目,最后一个
myDataGridEvtCode.SelectedItem=myDataGridEvtCode.Items.FirstOrDefault()
,用于选择最后一个
myDataGridEvtCode.SelectedItem=myDataGridEvtCode.Items.lastorefault()
。如果集合为空,它不会抛出异常,就像您当前的代码那样。如果它是空的,它将只返回
null
。也有
.First()
.Last()
可用。这些抛出异常。我只有四行两列!我的收藏很少!我认为这是一个代码问题。您是否使用标准的
DataGrid
?或者第三方网格?是的,标准数据网格!所以请注意我的答案,我希望这对你有用。谢谢,我尝试了你的示例代码,看起来不错!我将尝试在几个小时内修改我的代码:)谢谢!
<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;
        }
    }
}