WP7 中的 ListBox 偏移量 [英] 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?
推荐答案
您可以获得 ListBox 的 ScrollViewer 并使用它的 ScrollToVerticalOffset-method. 要获得 ScrollViewer,您可以例如连接到 ListBox 的 Loaded-event,如以下:
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 中的 ListBox 偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!