[英]Keep focus on a row after DataGrid.ItemsSource changed
I'd like to keep the focus on the selected row after the ItemsSource has changed. 在ItemsSource更改后,我想将焦点放在所选行上。 I think I'm almost done, but none of some accepted answers does work for me :(
我想我差不多完成了,但是没有一些接受的答案对我有用:(
This is my code: 这是我的代码:
public void UpdateItemsSource()
{
IdentifySelectedError(); //get "selectedRowIndex"
DataGridRow dgr = (DataGridRow)DataGrid.ItemContainerGenerator.ContainerFromIndex(selectedRowIndex);
using (var context = new Context())
{
DataGrid.ItemsSource = context.Error.Take(100).ToList();
}
//Everything does not work:
//DataGrid.SelectedIndex = selectedRowIndex;
//dgr.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
//dgr.Focus();
//DataGrid.Rows(dgr).Selected = true;
//DataGrid.ScrollIntoView(dgr);
//FocusManager.SetIsFocusScope(dgr, true);
}
What am I doing wrong? 我究竟做错了什么?
EDIT: 编辑:
I noticed that when I changed the ItemsSource, the selectedRowInde has the value 0. This is why it cannot set the focus on the previously selected Row. 我注意到当我更改ItemsSource时,selectedRowInde的值为0.这就是为什么它不能将焦点设置在先前选择的Row上。 Any ideas how to catch that?
任何想法如何捕获?
Save the row index into another int and set the selected item to it (or something like that): 将行索引保存到另一个int并将所选项设置为它(或类似的东西):
public void UpdateItemsSource()
{
IdentifySelectedError(); //get "selectedRowIndex"
using (var context = new Context())
{
DataGrid.ItemsSource = context.Error.Take(100).ToList();
}
MainDataGrid.SelectedItem = MainDataGrid.Items[indexOfRow];
MainDataGrid.ScrollIntoView(MainDataGrid.Items[indexOfRow]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.