开发者

wpf scrollviewer scrolltoverticaloffset

The events scrolltoVertica开发者_如何转开发lOffset or the scrolltoHorizontalOffset do not change the values of the scrollviewer. Please tell me at which event does the values HorizontalOffset and the VerticalOffset get changed? I have tried LayoutUpdated() method but it goes in a infinite loop.

Thanks in advance


In general the values of HorizontalOffset and VerticalOffset are not updated except during the LayoutUpdated event after the ScrollContentPresenter (or other IScrollInfo) has updated its value and called InvalidateScrollInfo(). The one exception is that the DependencyProperty for each of these is updated during deferred scrolling (but surprisingly the corresponding CLR property is not updated), but this probably does not apply in your case.

There are no ScrollToHorizontalOffset or ScrollToVerticalOffset events in WPF, but there is both a ScrollViewer method and a RoutedCommand of these names. Both the command version and the method version remember your request and execute it at the next LayoutUpdated event, so if all you want to do is make sure the scrolling happens, just send the command or call the method.

If you want to verify that HorizontalOffset or VerticalOffset has indeed been updated as desired you can simply catch the ScrollChangedEvent, which fires after the values have been updated, like this:

scrollViewer.ScrollChanged += (obj, e) =>
{
  // Get offset information from 'e' or from scrollViewer
}

I did not understand what you meant by "I have tried LayoutUpdated() method but it goes in a infinite loop," since you didn't explain what "LayoutUpdated() method" is, but the above information should make the order of events clear and help you on your way to a solution. In any case, all the information you need to make your decision should be available from the ScrollChanged event.


I ran into the same problem, thanks for posting the solution. The LayoutUpdated() method is called by the framework in an infinite loop, when you use ScrollChanged() instead of LayoutUpdated() it fixes the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜