I have an application where I need to take a certain action when the user gets to a certain place in a ScrollViewer
. This action sometimes includes scrolling the ScrollViewer
to a different location programmatically.
In order to moniter the user's scrolling action, I am listening for the ViewChanged
event of the ScrollViewer
. The issue is that when I scroll progrmatically from within the ViewChanged
event handler, that same event handler ends up getting called again, causing undesired additional scrolling to happen.
I have tried creating a custom method to remove the event handler before calling ScrollViewer.ChangeView()
, but this seems to have no effect.
Can anyone come up with a way around this issue, or a way to differentiate the user's scrolling action from my programmatic one?
private void MyScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
if (conditionals)
{
ScrollTo(location);
}
}
private void ScrollTo(double offset)
{
MyScrollViewer.ViewChanged -= MyScrollViewer_ViewChanged;
MyScrollViewer.ChangeView(offset, null, null);
MyScrollViewer.ViewChanged += MyScrollViewer_ViewChanged;
}