在 DataGrid.ItemsSource 更改后保持关注一行
Keep focus on a row after DataGrid.ItemsSource changed
我想在 ItemsSource 更改后将焦点保持在所选行上。我想我快完成了,但是 none 一些已接受的答案对我有用:(
这是我的代码:
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);
}
我做错了什么?
编辑:
我注意到当我更改 ItemsSource 时,selectedRowInde 的值为 0。这就是为什么它不能将焦点设置在先前选择的行上。有什么想法可以捕捉到它吗?
将行索引保存到另一个 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]);
}
我想在 ItemsSource 更改后将焦点保持在所选行上。我想我快完成了,但是 none 一些已接受的答案对我有用:(
这是我的代码:
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);
}
我做错了什么?
编辑:
我注意到当我更改 ItemsSource 时,selectedRowInde 的值为 0。这就是为什么它不能将焦点设置在先前选择的行上。有什么想法可以捕捉到它吗?
将行索引保存到另一个 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]);
}