views:

994

answers:

3

I tried following, all of following fails on function ScrollIntoView and gives NullReferenceException.

// doesnt work
grid.SelectedItem = sItem;
grid.ScrollIntoView(sItem);

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

// doesnt work
grid.SelectedItem = sItem;
grid.UpdateLayout();
grid.ScrollIntoView(sItem);

The problem is, when I select row from codebehind, selection is not visible its somewhere down in bottom, unless user scrolls he feels that selection has vanished. I need to scroll datagrid to the point user can see the selection.

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

+3  A: 

Try:

grid.SelectedItem = sItem; 
grid.UpdateLayout();
grid.ScrollIntoView(grid.SelectedItem);
Keith
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
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
Akash, have you given this a go yet?
Keith
Sorry Keith, didnt work for me, its virtualization issue.
Akash Kava
It works quite well, thanks!
Greg R
A: 

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

Akash Kava
A: 

Hello All.. can anyone help me with the same problem. I need to scroll the grid from the code.

grid.UpdateLayout, and grid.ScrollIntoView is not working for me. Please help
my SelectedItem is bound to the grid and is set from code behind. now i want the grid to scroll show that the particular entry is highlighted.