我还会为您的滚动查看器投票支持 AttachedProperty。
我创建了以下附加属性以将滚动绑定到以布尔变量结尾。
public static class ScrollViewerBehavior
{
public static readonly DependencyProperty ScrollToRightEndProperty =
DependencyProperty.RegisterAttached("ScrollToRightEnd", typeof (bool), typeof (ScrollViewerBehavior),
new PropertyMetadata(false, OnScrollToRightEndPropertyChanged));
public static bool GetScrollToRightEnd(DependencyObject obj)
{
return (bool) obj.GetValue(ScrollToRightEndProperty);
}
public static void SetScrollToRightEnd(DependencyObject obj, bool value)
{
obj.SetValue(ScrollToRightEndProperty, value);
}
private static void OnScrollToRightEndPropertyChanged(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
var sv = sender as ScrollViewer;
if (sv == null) return;
if ((bool) e.NewValue)
{
sv.ScrollToRightEnd();
}
else
{
sv.ScrollToLeftEnd();
}
}
}
您可以在 XAML 中使用此附加属性...
<ScrollViewer ... local:ScrollViewerBehavior.ScrollToRightEnd="{Binding IsScrolledToEnd}" ... />
或者,如果您想保存问题中的操作委托,您可以在上面的 OnScrollToRightEndPropertyChanged 方法中执行以下操作。
.....
var viewModel = sv.DataContext as YourViewModel;
if (viewModel != null)
{
viewModel.ScrollAction = () => sv.ScrollToRightEnd();
}
.....