This is a migrated thread and some comments may be shown as answers.

[Solved] Maintain Horizontal scroll position after calling scrollintoviewAsync method

6 Answers 318 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Rajesh
Top achievements
Rank 1
Rajesh asked on 31 Jan 2011, 07:02 PM
Hi Telerik Admin,

I am calling scrollintoviewAsync  method of gridview and it is moving scroll to correct row position by changing vertical scroll bar position.
It also changes horizontal scroll position.I want to maintain horizontal scroll position.how i can keep horizontal scroll bar position
Please suggest.

Thanks,
Rajesh

6 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 01 Feb 2011, 10:55 AM

Hello Rajesh,

You can try to restore the horizontal offset once the vertical scrolling is complete.

double lastHorizontalOffset = 0.0;
  
private void Button1_Click(object sender, RoutedEventArgs e)
{
    var scrollViewer = this.playersGrid.ChildrenOfType<GridViewScrollViewer>().FirstOrDefault();
    this.lastHorizontalOffset = scrollViewer.HorizontalOffset;
  
    this.playersGrid.ScrollIntoViewAsync(this.playersGrid.Items[5], new Action<FrameworkElement>(OnScrolled));
}
  
private void OnScrolled(FrameworkElement obj)
{
    var scrollViewer = this.playersGrid.ChildrenOfType<GridViewScrollViewer>().FirstOrDefault();
    scrollViewer.ScrollToHorizontalOffset(this.lastHorizontalOffset);
}



All the best,
Milan
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Rajesh
Top achievements
Rank 1
answered on 01 Feb 2011, 11:08 AM
Thanks Milan for your solution.

I am already  using similar kind of code.

 

 

double offset= scrollViewer.HorizontalOffset;

 

 

 

this.radcontrol.ScrollIntoViewAsync(this.radcontrol.Items[recordIndex],

 

 

 

new Action<FrameworkElement>((f) =>

 

{

 

 

scrollViewer.ScrollToHorizontalOffset( offset);
}
problem with this is that i can see that horizontal scroll viewer is coming to start position and then again it is restoring its previous position.I don't want to move horizontal scroll bar to start position and then move that to original position.I want to keep same horizontal scroll position throughout the cycle.

Thanks,
Rajesh

 

0
Milan
Telerik team
answered on 01 Feb 2011, 11:26 AM

Hi Rajesh,

Unfortunately, there is not way to restrict the movement of the horizontal bar when using ScrollIntoView. You could also try this custom implementation of SrollIntoView. The custom approach has it limitations as well = for example, it will not work correctly if rows have different size. In addition to that it will not work when the grid is grouped. 



Regards,
Milan
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Rajesh
Top achievements
Rank 1
answered on 01 Feb 2011, 12:48 PM
Thanks for your Quick Response Milan . " You could also try this custom implementation of SrollIntoView" here are you pointing to some link or are you referring the code snippet i have mentioned.

Thanks,
Rajesh
0
Milan
Telerik team
answered on 01 Feb 2011, 01:23 PM
Hi Rajesh,

I forgot to paste the code snippet. Excuse me for that. 

private void ScrollIntoViewCustom(object item)
{
    var itemIndex = this.playersGrid.Items.IndexOf(item);
    var scrollViewer = this.playersGrid.ChildrenOfType<GridViewScrollViewer>().FirstOrDefault();
    var firstRow = scrollViewer.ChildrenOfType<GridViewRow>().Where(r => !(r is GridViewNewRow)).FirstOrDefault();
    var newItemOffset = itemIndex * firstRow.RenderSize.Height;
  
    scrollViewer.ScrollToVerticalOffset(newItemOffset);
}

Greetings,
Milan
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Rajesh
Top achievements
Rank 1
answered on 01 Feb 2011, 01:53 PM
Thanks Milan,
In my case i have few row details item open.So i think it will not work.Let me know if you have other thought on this,
Tags
GridView
Asked by
Rajesh
Top achievements
Rank 1
Answers by
Milan
Telerik team
Rajesh
Top achievements
Rank 1
Share this question
or