我在WPF中创建了一个带有几行的数据网格。
我在我的wpf网格上创建了四个按钮,以便在行之间导航:[<<] - [<] - [>] - [>>]
我使用SelectedItem函数来设置行。 我的问题是,突出显示似乎出现了问题(速度很慢),出现起来有点困难。 当我使用键盘箭头(向上和向下)在行之间导航时,突出显示是快速和立即的。但是,在我的代码后面的按钮中,突出显示有点慢和奇怪。
这是我的代码:
我使用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();
}
有人对此有一些想法吗?
非常感谢我的朋友们:)
myDataGridEvtCode.SelectedItem = myDataGridEvtCode.Items.FirstOrDefault()
,对于最后一个则是myDataGridEvtCode.SelectedItem = myDataGridEvtCode.Items.LastOrDefault()
。如果集合为空,它不会像你当前的代码一样抛出异常,而只会返回null
。还有.First()
和.Last()
可用,但这些会抛出异常。 - Michael SchnerringDataGrid
吗?还是使用第三方的表格控件? - Michael Schnerring