WP7中的列表框偏移 [英] ListBox offset in WP7
问题描述
是否可以设置ListBox的偏移量?我所能找到的只是滚动到一个元素,但我需要将ListBox滚动到任何位置.
It is possible set the offset for a ListBox? All that I can find is scroll to an element, but I need to scroll the ListBox to any position.
作为替代方案,还有其他任何组件可以虚拟化其项目,并且我可以控制偏移量吗?
As an alternative, there are any other component that can make virtualize their items, and that I can control the offset?
推荐答案
You can get the ListBox's ScrollViewer and use its ScrollToVerticalOffset-method. To get the ScrollViewer, you can for example hook up to the ListBox's Loaded-event like the following:
XAML:
<ListBox Loaded="HookScrollViewer">
隐藏代码:
private void HookScrollViewer(object sender, RoutedEventArgs e)
{
var element = (FrameworkElement)sender;
var scrollViewer = ControlHelpers.FindChildOfType<ScrollViewer>(element);
if (scrollViewer == null)
return;
this.myScrollViewer = scrollViewer;
}
ControlHerlpers.FindChildOfType方法是通过以下方式实现的:
The ControlHerlpers.FindChildOfType-method is implement this way:
public static T FindChildOfType<T>(DependencyObject root) where T : class
{
var queue = new Queue<DependencyObject>();
queue.Enqueue(root);
while (queue.Count > 0)
{
var current = queue.Dequeue();
for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)
{
var child = VisualTreeHelper.GetChild(current, i);
var typedChild = child as T;
if (typedChild != null)
{
return typedChild;
}
queue.Enqueue(child);
}
}
return null;
}
现在,在myScrollViewer成员中具有ListBox的ScrollViewer,您可以直接访问其方法.例如,要滚动到底部,您可以调用:
Now you have the ListBox's ScrollViewer in the myScrollViewer member and you can directly access its methods. For example, to scroll bottom you can call:
this.myScrollViewer.ScrollToVerticalOffset(double.MaxValue);
这篇关于WP7中的列表框偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!