简体   繁体   English

如何以编程方式滚动 WPF 列表视图?

[英]How can I programmatically scroll a WPF listview?

Is it possible to programmatically scroll a WPF listview?是否可以以编程方式滚动 WPF 列表视图? I know winforms doesn't do it, right?我知道 winforms 不会这样做,对吗?

I am talking about say scrolling 50 units up or down, etc. Not scrolling an entire item height at once.我说的是向上或向下滚动 50 个单位等。而不是一次滚动整个项目高度。

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.是的,您必须从 ListView 中获取 ScrollViwer,或者一旦您有权访问它,您就可以使用它公开的方法或覆盖滚动。 You can also scroll by getting the main content area and using it's implementation of the IScrollInfo interface.您还可以通过获取主要内容区域并使用它的IScrollInfo接口实现来滚动。

Here's a little helper to get the ScrollViwer component of something like a ListBox, ListView, etc.这是一个小帮手,可以获取 ListBox、ListView 等的 ScrollViwer 组件。

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:然后你可以像这样使用 .LineUp() 和 .LineDown() :

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. LineUp 和 LineDown 公开的逻辑滚动仍然按项目滚动,如果你想滚动一定数量,你应该使用我上面使用的 ScrollToHorizontal/VerticalOffset。 If you want some more complex scrolling too, then take a look at the answer I've provided in this other question .如果您还想要一些更复杂的滚动,请查看我在另一个问题中提供的答案。

Have you tried ScrollIntoView ?你试过ScrollIntoView吗? Alternatively, if it's not a specific item you brought into view, but an offset from the current position, you can use BringIntoView .或者,如果它不是您带入视图的特定项目,而是当前 position 的偏移量,您可以使用BringIntoView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C#以编程方式在WPF中创建一个包含字符串的ListView,该字符串包含行和列? - How can I programmatically create a ListView full of strings containing columns and rows in WPF using C#? 使用 WPF,如何以编程方式禁用在 XAML 中为 Listview 控件创建的按钮? - Using WPF, how can I programmatically disable buttons that were created in XAML for a Listview control? 如何以编程方式滚动listview项目 - How to scroll listview items programmatically 如何在WPF中以编程方式滚动网格? - How can I scroll the grid programatically in WPF? WPF无法在Listview WPF中滚动ItemsControl - WPF Can't scroll ItemsControl inside a Listview WPF 如何以编程方式为click事件添加listView列标题 - How can I add a listView column header a click event programmatically 如何在WPF(故事板动画)中以编程方式更改矩形的边框? - How can I programmatically change the border of a rectangle in WPF (storyboard animation)? 如何以编程方式移动到WPF TabControl中的下一个选项卡? - How can I programmatically move to the next tab in a WPF TabControl? 如何在WPF代码中以编程方式实现这一行XAML? - How can I implement this one line XAML programmatically in WPF code? 如何以编程方式更新Listview(wpf)中特定行中的特定列 - How do i update a specific column in a specific row in Listview (wpf) programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM