Reputation: 389
I'd like to keep the focus on the selected row after the ItemsSource has changed. 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. Any ideas how to catch that?
Upvotes: 3
Views: 657
Reputation: 703
Save the row index into another int and set the selected item to it (or something like that):
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]);
}
Upvotes: 1