WPF scrollviewer changes offset when switching between tabs

WPF

When switching tabs, the scrollviewer position might change. In WPF when a new tab selected, the previous inactive tab is unloaded, so when it is loaded again the previous position is lost.

To handle it, we must catch the scroll viewer unload and load events, keep the current offset and restore it.



double _currentVerticalOffset;

private void scroller_Unloaded(object sender, RoutedEventArgs e)
{
	_currentVerticalOffset = scrollVwr.VerticalOffset;
}

private void scrollVwr_Loaded(object sender, RoutedEventArgs e)
{
	scroller.ScrollToVerticalOffset(_currentVerticalOffset);
}

Post a Comment

Previous Post Next Post