WPF - 动画ListBox.ScrollViewer.HorizontalOffset? [英] WPF - Animate ListBox.ScrollViewer.HorizontalOffset?
问题描述
我集合在
秒。我需要找到的ListBox
视觉 xPosition位置
里面的元素,然后动画的 HorizontalOffset
的列表框
的的ScrollViewer
。基本上我想创建一个动画 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
取值的ScrollViewer参考?其次,我怎样才能在 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
?
我不reponding上的的ListBox
本身,所以我不能用鼠标
相关属性的任何输入
I'm not reponding to any input on the ListBox
itself so I can't use Mouse
related properties.
推荐答案
我不认为你将能够使用WPF故事板的动画故事板,因为动画WPF依赖属性。您将需要调用 ScrollViewer.ScrollToHorizontalOffset(双)
滚动。
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搜索列表框的视觉儿童的滚动浏览器。保存到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&LT;的ScrollViewer方式&gt;(列表框)
来获得的ScrollViewer
This function returns the first visual child of the parameter type. Call FindVisualChild<ScrollViewer>(ListBox)
to get the ScrollViewer.
最后,请尝试使用 UIElement.TranslatePoint(点,的UIElement)
来获得该项目的X位置。呼吁项目这一功能,在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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!