Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

c# - How can I programmatically scroll a WPF listview?

Is it possible to programmatically scroll a WPF listview? I know winforms doesn't do it, right?

I am talking about say scrolling 50 units up or down, etc. Not scrolling an entire item height at once.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, you'll have to grab the ScrollViwer from the ListView, or but once you have access to that, you can use the methods exposed by it or override the scrolling. You can also scroll by getting the main content area and using it's implementation of the IScrollInfo interface.

Here's a little helper to get the ScrollViwer component of something like a ListBox, ListView, etc.

public static DependencyObject GetScrollViewer(DependencyObject o)
{
    // Return the DependencyObject if it is a ScrollViewer
    if (o is ScrollViewer)
    { return o; }

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(o); i++)
    {
        var child = VisualTreeHelper.GetChild(o, i);

        var result = GetScrollViewer(child);
        if (result == null)
        {
            continue;
        }
        else
        {
            return result;
        }
    }
    return null;
}

And then you can just use .LineUp() and .LineDown() like this:

private void OnScrollUp(object sender, RoutedEventArgs e)
{
    var scrollViwer = GetScrollViewer(uiListView) as ScrollViewer;

    if (scrollViwer != null)
    {
       // Logical Scrolling by Item
       // scrollViwer.LineUp();
       // Physical Scrolling by Offset
       scrollViwer.ScrollToVerticalOffset(scrollViwer.VerticalOffset + 3);
    }
}

private void OnScrollDown(object sender, RoutedEventArgs e)
{
    var scrollViwer = GetScrollViewer(uiListView) as ScrollViewer;

    if (scrollViwer != null)
    {
        // Logical Scrolling by Item
        // scrollViwer.LineDown();
        // Physical Scrolling by Offset
        scrollViwer.ScrollToVerticalOffset(scrollViwer.VerticalOffset + 3);
    }
}


<DockPanel>
    <Button DockPanel.Dock="Top"
            Content="Scroll Up"
            Click="OnScrollUp" />
    <Button DockPanel.Dock="Bottom"
            Content="Scroll Down"
            Click="OnScrollDown" />
    <ListView x:Name="uiListView">
        <!-- Content -->
    </ListView>
</DockPanel>

The Logical scrolling exposed by LineUp and LineDown do still scroll by item, if you want to scroll by a set amount you should use the ScrollToHorizontal/VerticalOffset that I've used above. If you want some more complex scrolling too, then take a look at the answer I've provided in this other question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...