【问题标题】:WPF Listbox scroll downWPF 列表框向下滚动
【发布时间】:2009-03-16 11:51:58
【问题描述】:

假设我有一个包含许多项目的列表框,以便出现垂直滚动,但我隐藏了滚动条

ScrollViewer.VerticalScrollBarVisibility="Hidden"

有什么方法可以为我添加一个向下滚动的按钮? iv 尝试添加

Command="ScrollBar.LineDownCommand" 

到一个按钮,但没有任何效果。

【问题讨论】:

    标签: wpf scrollbar


    【解决方案1】:

    您需要告诉 WPF 从何处开始查找命令处理程序。不告诉它,它将从Button 开始查找,并且找不到任何处理LineDownCommand 的东西。不幸的是,将其设置为 ListBox 是不够的,因为 ScrollViewer ListBox 作为其模板的一部分,因此 WPF 仍然找不到它。

    将其设置为 ListBoxItems 之一是不合适的,但可以:

    <Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
    
            <ListBox x:Name="_listBox" ScrollViewer.VerticalScrollBarVisibility="Hidden">
                <ListBoxItem x:Name="_listBoxItem">One</ListBoxItem>
                <ListBoxItem>Two</ListBoxItem>
                <ListBoxItem>Three</ListBoxItem>
                <ListBoxItem>One</ListBoxItem>
                <ListBoxItem>Two</ListBoxItem>
                <ListBoxItem>Three</ListBoxItem>
                <ListBoxItem>One</ListBoxItem>
                <ListBoxItem>Two</ListBoxItem>
                <ListBoxItem>Three</ListBoxItem>
                <ListBoxItem>One</ListBoxItem>
                <ListBoxItem>Two</ListBoxItem>
                <ListBoxItem>Three</ListBoxItem>
            </ListBox>
            <Button Grid.Row="1" Command="ScrollBar.LineDownCommand" CommandTarget="{Binding ElementName=_listBoxItem}">Scroll Down</Button>
        </Grid>
    </Window>
    

    更好的方法是重新模板化ListBox 并将Button 粘贴在模板内,或者在代码隐藏中连接CommandTarget

    【讨论】:

      【解决方案2】:

      我有一个应用程序,我想手动控制 ScrollViewer 的滚动。基本上,我得到了 ScrollViewer 的引用,然后使用 ScrollToHorizo​​ntalOffset() 方法来控制滚动。以下是我解释我使用的过程的博客文章:

      http://www.developingfor.net/wpf/fun-with-the-wpf-scrollviewer.html

      http://www.developingfor.net/wpf/more-fun-with-wpf-scrollviewer.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-21
        • 2020-10-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-25
        • 2018-05-08
        相关资源
        最近更新 更多