locked
Move focus to element in scrollviewer RRS feed

  • Question

  • Hi All,

    I have some input elements in stackpanel and stackpanel is in scrollviewer. In the left i have some links. Now if user clicks on link, then i have to move scroll viewer and also have to set the focus of particular control.

    Actually i want bookmarking functionality like in HTML.

     

    Any help would be appreciated.

    Regards

    Dheeraj Sharma

    Tuesday, May 11, 2010 7:48 AM

Answers

  • Hi,

    I wouldn't try to force this. Just imagine the control you want to focus on is the last one, then there'd be no way to move it up to the first position, because there is nothing behind it that could fill the space. So, trying to force the control to the top position will always be inconsistent, because you would have to calculate the position somehow, and would have to make exceptions for the last few elements, anyway.

    So much said, you could try to set CanContentScroll to true. This changes the behaviour of ScrollToVerticalOffset: instead of a pixel length, the offset will be the index of the element that has the top position in the ScrollViewer. So, if you know the index of an element in the collection, you can scroll it to the top with ScrollToVerticalOffset(index), unless you reach the limit of the offset, as I wrote before.


    http://wpfglue.wordpress.com
    • Marked as answer by Jie Bao Tuesday, May 18, 2010 1:03 AM
    Tuesday, May 11, 2010 10:46 AM

All replies

  • Hi,

    just call the element's BringIntoView method. If the element is contained in a StackPanel and the StackPanel in a ScrollViewer, this should do what you want.


    http://wpfglue.wordpress.com
    Tuesday, May 11, 2010 8:12 AM
  • Hi

    Thanks for reply.

    I have tried it and it works. But it does not scroll it to top.  How can i scroll element to top in scrollviewer?

    Tuesday, May 11, 2010 8:41 AM
  • Hi,

    I wouldn't try to force this. Just imagine the control you want to focus on is the last one, then there'd be no way to move it up to the first position, because there is nothing behind it that could fill the space. So, trying to force the control to the top position will always be inconsistent, because you would have to calculate the position somehow, and would have to make exceptions for the last few elements, anyway.

    So much said, you could try to set CanContentScroll to true. This changes the behaviour of ScrollToVerticalOffset: instead of a pixel length, the offset will be the index of the element that has the top position in the ScrollViewer. So, if you know the index of an element in the collection, you can scroll it to the top with ScrollToVerticalOffset(index), unless you reach the limit of the offset, as I wrote before.


    http://wpfglue.wordpress.com
    • Marked as answer by Jie Bao Tuesday, May 18, 2010 1:03 AM
    Tuesday, May 11, 2010 10:46 AM
  • Hi,

    You are right about the last element.

    I have solved this by using CustomScrollViewer( http://aniscrollviewer.codeplex.com/).

    Thanks for your valuable help.

    Regards

    Dheeraj

    • Proposed as answer by Krementsov Tuesday, November 6, 2018 1:59 PM
    Tuesday, May 11, 2010 1:19 PM