问题描述
我在 ListBox
中有一组 Visual
.我需要在其中找到元素的 XPosition
,然后为 ListBox
的 ScrollViewer
的 HorizontalOffset
设置动画.基本上我想创建一个动画 ScrollIntoView
方法.
I have a collection of Visual
s 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
中任意元素的相对 XPosition
或 HozintalOfffset
?
This gives me a couple of problems. Firstly, how can I get a reference to the ListBox
s 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.ScrollToHorizontalOffset(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() 函数中创建一个调用 SetHorizontalOffset 的自定义依赖属性.然后你可以为这个属性设置动画.
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.HorizontalOffset?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!