以编程方式选择 wpf 数据网格行并突出显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15121723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select a wpf datagrid row programmaticaly with the highlighting
提问by Walter Fabio Simoni
I created a datagrid in WPF with a few rows. I created four buttons on my wpf grid in order to navigate between the rows : [<<] -- [<] -- [>] -- [>>]
我在 WPF 中创建了一个包含几行的数据网格。我在 wpf 网格上创建了四个按钮,以便在行之间导航:[<<] -- [<] -- [>] -- [>>]
I use the SelectedItem function in order to set the rows. My problem is that the highlighting appear to be bad ( slow ) to appear ( it's a bit difficult to explain ).
我使用 SelectedItem 函数来设置行。我的问题是突出显示似乎不好(慢)出现(有点难以解释)。
When i use the keybord arrow (up and down) in order to between the rows, the highlighting is fast and immediate. Whit my code behind my button, the highlighting is a bit slow and strange.
当我使用键盘箭头(向上和向下)在行之间时,突出显示是快速而直接的。在我的按钮后面的代码中,突出显示有点缓慢和奇怪。
Here is my code
这是我的代码
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();
}
Anyone have some ideas about this ?
有人对此有一些想法吗?
Thanks a lot my friends :)
非常感谢我的朋友:)
回答by Michael Schnerring
I assume, you use System.Windows.Control.DataGrid. I didn't try your code. Here's some code, I just threw together in a samlple WPF app. Selection through pressing the buttons works as fluently as selecting rows manually with my mouse/keyboard.
我假设,您使用System.Windows.Control.DataGrid. 我没有尝试你的代码。这是一些代码,我只是在一个示例 WPF 应用程序中拼凑起来。通过按下按钮进行选择与使用鼠标/键盘手动选择行一样流畅。
XAML
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>
Code behind
背后的代码
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;
}
}
}
Working with indicies might be the clue. Though I haven't searched for a proof, yet.
使用指数可能是线索。虽然我还没有寻找证据。

