开发者

WPF : How to scroll a ListView control horizontal?

I manually need to scroll the content of a ListView control to the left side.

It is invoked automatically when I call scrollIntoView but only if the item to scroll to is not visible. The ListView will scroll to the item and scroll horizontal to the left side. Just like I need it to be.

But if the item to scroll to is already visible nothing will happen and that is the reaso开发者_开发百科n I need to scroll left manually.


You could find the ScrollViewer for the ListView by traversing the Visual Tree and then call ScrollToLeftEnd. Something like this should work

private void ScrollListViewToLeft(ListView listView)
{
    ScrollViewer listViewScrollViewer = GetVisualChild<ScrollViewer>(listView);
    listViewScrollViewer.ScrollToLeftEnd();
}
private static T GetVisualChild<T>(DependencyObject parent) where T : Visual
{
    T child = default(T);
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
        if (child != null)
        {
            break;
        }
    }
    return child;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜