WPF - 动画 ListBox.ScrollViewer.Horizo​​ntalOffset? [英] WPF - Animate ListBox.ScrollViewer.HorizontalOffset?

查看:28
本文介绍了WPF - 动画 ListBox.ScrollViewer.Horizo​​ntalOffset?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ListBox 中有一组 Visual.我需要在其中找到元素的 XPosition,然后为 ListBoxScrollViewerHorizo​​ntalOffset 设置动画.基本上我想创建一个动画 ScrollIntoView 方法.

I have a collection of Visuals in a ListBox. I need to find the XPosition of an element inside it and then animate the HorizontalOffset of the ListBox's ScrollViewer. Essentially I want to created an animated ScrollIntoView method.

这给我带来了一些问题.首先,如何获得对 ListBox 的滚动查看器的引用?其次,如何获得 ListBox 中任意元素的相对 XPositionHozintalOfffset ?

This gives me a couple of problems. Firstly, how can I get a reference to the ListBoxs scrollviewer? Secondly, how can i get the relative XPosition or HozintalOfffset of an arbitrary element in the ListBox?

我不会响应 ListBox 本身的任何输入,因此我无法使用 Mouse 相关属性.

I'm not reponding to any input on the ListBox itself so I can't use Mouse related properties.

推荐答案

我认为您不能将 WPF 故事板用于动画,因为故事板会为 WPF 依赖项属性设置动画.您需要调用 ScrollViewer.ScrollToHorizo​​ntalOffset(double) 来滚动.

I don't think you will be able to use a WPF storyboard for the animation because storyboards animate WPF dependency properties. You will need to call ScrollViewer.ScrollToHorizontalOffset(double) to scroll.

您可以尝试在 OnDependencyPropertyChanged() 函数中创建一个调用 SetHorizo​​ntalOffset 的自定义依赖属性.然后你可以为这个属性设置动画.

You could try creating a custom dependency property that calls SetHorizontalOffset in the OnDependencyPropertyChanged() function. Then you could animate this property.

public static readonly DependencyProperty ScrollOffsetProperty =
   DependencyProperty.Register("ScrollOffset", typeof(double), typeof(YOUR_TYPE),
   new FrameworkPropertyMetadata(0.0, new PropertyChangedCallback(OnScrollOffsetChanged)));


public double ScrollOffset
{
   get { return (double)GetValue(ScrollOffsetProperty); }
   set { SetValue(ScrollOffsetProperty, value); }
}

private static void OnScrollOffsetChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
   YOUR_TYPE myObj = obj as YOUR_TYPE;

   if (myObj != null)
      myObj.SCROLL_VIEWER.ScrollToHorizontalOffset(myObj.ScrollOffset);
}

要获得滚动查看器,您可以使用 VisualTreeHelper 来搜索 ListBox 的可视子项.保存对 ScrollViewer 的引用,因为稍后您将需要它.试试这个:

To get the scroll viewer you can use the VisualTreeHelper to search the visual children of the ListBox. Save a reference to the ScrollViewer because you will need it later. Try this:

public static childItem FindVisualChild<childItem>(DependencyObject obj)
   where childItem : DependencyObject
{
   // Iterate through all immediate children
   for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
   {
      DependencyObject child = VisualTreeHelper.GetChild(obj, i);

      if (child != null && child is childItem)
         return (childItem)child;

      else
      {
         childItem childOfChild = FindVisualChild<childItem>(child);

         if (childOfChild != null)
            return childOfChild;
      }
   }

   return null;
}

此函数返回参数类型的第一个可视子项.调用 FindVisualChild(ListBox) 以获取 ScrollViewer.

This function returns the first visual child of the parameter type. Call FindVisualChild<ScrollViewer>(ListBox) to get the ScrollViewer.

最后,尝试使用 UIElement.TranslatePoint(Point, UIElement) 来获取项目的 X 位置.在item上调用这个函数,为point传入0,0,传入ScrollViewer.

Finally, try using UIElement.TranslatePoint(Point, UIElement) to get the X position of the item. Call this function on the item, pass in 0,0 for the point, and pass in the ScrollViewer.

希望这会有所帮助.

这篇关于WPF - 动画 ListBox.ScrollViewer.Horizo​​ntalOffset?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆