1

我创建了一个名为“AniScrollViewer”的类,它可以设置垂直滚动偏移
来动画 Scrollviewer 代码隐藏。

public class AniScrollViewer : ScrollViewer {
    public static readonly DependencyProperty CurrentVerticalOffsetProperty =
        DependencyProperty.Register("CurrentVerticalOffset", typeof(double), typeof(AniScrollViewer),
        new PropertyMetadata(new PropertyChangedCallback(OnVerticalChanged)));

    public double CurrentVerticalOffset {
        get { return (double)GetValue(CurrentVerticalOffsetProperty); }
        set { SetValue(CurrentVerticalOffsetProperty, value); }
    }

    private static void OnVerticalChanged(DependencyObject property, DependencyPropertyChangedEventArgs e) {
        AniScrollViewer viewer = property as AniScrollViewer;
        viewer.ScrollToVerticalOffset((double)e.NewValue);
    }
}

我想将此“AniScrollViewer”推入列表框

我遇到了几个问题:

  • Listbox 类内部已经有“scrollviewer”。我想
    用我的自定义 AniScrollViewer 替换默认的列表框滚动查看器,但我不知道该怎么做。
  • 我想在 Listbox 代码隐藏中获取 ScrollViewer。像这样:

Listbox listbox (这已经在 XAML 中定义)
AniScrollViewer scrollviewer = listbox.~~~~;

请帮忙。

4

1 回答 1

1
  • ScrollViewer使用自定义替换默认设置,请为 定义自定义StyleListBox然后使用您的自定义替换默认ControlTemplate设置ScrollViewerlocal:AniScrollViewer)。现在任何ListBox使用它Style的人都会拥有你的ScrollViewer. 默认ControlTemplateStyle可以在这里找到
  • 要获取ScrollViewer代码隐藏,您可以将用作参考。在函数中

GetScrollViewer(DependencyObject o)

检查它以确保您的自定义类型安全,ScrollViewer例如

if (o is AniScrollViewer)
  return o;

备用

但是,在您的情况下,我不确定您为什么选择为此子类化ScrollViewer。而不是定义自己的麻烦ListBox ControlTemplate,也许尝试使用附加属性。就像是:

public class ScrollAnimator {
  public static readonly DependencyProperty ScrollToProperty =
    DependencyProperty.RegisterAttached(
      "ScrollTo",
      typeof(double),
      typeof(ScrollAnimator),
      new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.None, ScrollToChangedCallback));

  private static void ScrollToChangedCallback(
    DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) {
    ScrollViewer viewer = GetScrollViewer(dependencyObject) as ScrollViewer;
    if (viewer != null)
      viewer.ScrollToVerticalOffset((double)dependencyPropertyChangedEventArgs.NewValue);

    // Modify Above code to however you want to do the animation.
  }

  public static DependencyObject GetScrollViewer(DependencyObject o) {
    if (o is ScrollViewer)
      return o;

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(o); i++) {
      var child = VisualTreeHelper.GetChild(o, i);
      var result = GetScrollViewer(child);
      if (result == null)
        continue;
      return result;
    }
    return null;
  }

  public static void SetScrollTo(UIElement element, Orientation value) {
    element.SetValue(ScrollToProperty, value);
  }

  public static Orientation GetScrollTo(UIElement element) {
    return (Orientation)element.GetValue(ScrollToProperty);
  }
}

和用法:

<ListBox ItemsSource="{Binding Items}"
         local:ScrollAnimator.ScrollTo="{Binding ScrollTo}" />

这样您就不需要定义 customControlTemplate以及随之而来的所有开销。

于 2013-06-03T08:11:08.313 回答