16

I tried the following, all of which fail on function ScrollIntoView and give a NullReferenceException:

// doesn't work
grid.SelectedItem = sItem;
grid.ScrollIntoView(sItem);

// doesn't work
grid.SelectedItem = sItem;
grid.Focus();
grid.CurrentColumn = grid.Columns[0];
grid.UpdateLayout();
grid.ScrollIntoView(sItem,grid.Columns[0]);

// doesn't work
grid.SelectedItem = sItem;
grid.UpdateLayout();
grid.ScrollIntoView(sItem);

The problem is, when I select a row from code-behind, selection is not visible - it's somewhere down the bottom. Unless the user scrolls they feels that selection has vanished. I need to scroll a DataGrid to the point that user can see the selection.

I also tried "BringIntoView" as well but no luck.

David Gardiner
  • 16,892
  • 20
  • 80
  • 117
Akash Kava
  • 39,066
  • 20
  • 121
  • 167

2 Answers2

50

Try:

grid.SelectedItem = sItem; 
grid.UpdateLayout();
grid.ScrollIntoView(grid.SelectedItem);
Keith Harrison
  • 1,567
  • 14
  • 22
  • in first statement I am assigning grid.SelectedItem = sItem, do you think grid.SelectedItem and sItem are different in anyway?? sorry its wrong answer it will never work. – Akash Kava Dec 31 '09 at 10:52
  • SelectedItem wrapper on a dependency property so my code is different to what you have. Give it a try, I had the same issue and this worked for me. See dissembly: [Category("Appearance"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Bindable(true)] public object SelectedItem { get { return base.GetValue(SelectedItemProperty); } set { base.SetValue(SelectedItemProperty, value); } } – Keith Harrison Dec 31 '09 at 12:01
  • Akash, have you given this a go yet? – Keith Harrison Jan 04 '10 at 10:12
7

Virtualized Stack Panel didn't have an item container, because Item Container does not exist for the item outside the view and that's why this error was shown. Disabling virtualization resolves the issue for now, and the bug has been reported to codeplex toolkit project.

David Gardiner
  • 16,892
  • 20
  • 80
  • 117
Akash Kava
  • 39,066
  • 20
  • 121
  • 167