I started with this but noticed there isn't a cleanup phase so here goes the complete implementation with both horizontal and vertical offsets bindable:
using System.Windows;
using System.Windows.Controls;
namespace Test
{
public static class ScrollPositionBehavior
{
public static readonly DependencyProperty HorizontalOffsetProperty =
DependencyProperty.RegisterAttached(
"HorizontalOffset",
typeof(double),
typeof(ScrollPositionBehavior),
new FrameworkPropertyMetadata(
double.NaN,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
OnHorizontalOffsetPropertyChanged));
public static readonly DependencyProperty VerticalOffsetProperty =
DependencyProperty.RegisterAttached(
"VerticalOffset",
typeof(double),
typeof(ScrollPositionBehavior),
new FrameworkPropertyMetadata(
double.NaN,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
OnVerticalOffsetPropertyChanged));
private static readonly DependencyProperty IsScrollPositionBoundProperty =
DependencyProperty.RegisterAttached("IsScrollPositionBound", typeof(bool?), typeof(ScrollPositionBehavior));
public static void BindOffset(ScrollViewer scrollViewer)
{
if (scrollViewer.GetValue(IsScrollPositionBoundProperty) is true)
return;
scrollViewer.SetValue(IsScrollPositionBoundProperty, true);
scrollViewer.Loaded += ScrollViewer_Loaded;
scrollViewer.Unloaded += ScrollViewer_Unloaded;
}
public static double GetHorizontalOffset(DependencyObject depObj)
{
return (double)depObj.GetValue(HorizontalOffsetProperty);
}
public static double GetVerticalOffset(DependencyObject depObj)
{
return (double)depObj.GetValue(VerticalOffsetProperty);
}
public static void SetHorizontalOffset(DependencyObject depObj, double value)
{
depObj.SetValue(HorizontalOffsetProperty, value);
}
public static void SetVerticalOffset(DependencyObject depObj, double value)
{
depObj.SetValue(VerticalOffsetProperty, value);
}
private static void OnHorizontalOffsetPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ScrollViewer scrollViewer = d as ScrollViewer;
if (scrollViewer == null || double.IsNaN((double)e.NewValue))
return;
BindOffset(scrollViewer);
scrollViewer.ScrollToHorizontalOffset((double)e.NewValue);
}
private static void OnVerticalOffsetPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ScrollViewer scrollViewer = d as ScrollViewer;
if (scrollViewer == null || double.IsNaN((double)e.NewValue))
return;
BindOffset(scrollViewer);
scrollViewer.ScrollToVerticalOffset((double)e.NewValue);
}
private static void ScrollChanged(object s, ScrollChangedEventArgs se)
{
if (se.VerticalChange != 0)
SetVerticalOffset(s as ScrollViewer, se.VerticalOffset);
if (se.HorizontalChange != 0)
SetHorizontalOffset(s as ScrollViewer, se.HorizontalOffset);
}
private static void ScrollViewer_Loaded(object sender, RoutedEventArgs e)
{
var scrollViewer = sender as ScrollViewer;
scrollViewer.ScrollChanged += ScrollChanged;
}
private static void ScrollViewer_Unloaded(object sender, RoutedEventArgs e)
{
var scrollViewer = sender as ScrollViewer;
scrollViewer.SetValue(IsScrollPositionBoundProperty, false);
scrollViewer.ScrollChanged -= ScrollChanged;
scrollViewer.Loaded -= ScrollViewer_Loaded;
scrollViewer.Unloaded -= ScrollViewer_Unloaded;
}
}
}