2

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

ScrollViewer.VerticalScrollBarVisibility="Hidden"

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

Command="ScrollBar.LineDownCommand" 

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

4

2 回答 2

4

您需要告诉 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在代码隐藏中连接。

于 2009-03-16T12:26:31.027 回答
0

我有一个应用程序,我想手动控制 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

于 2009-03-17T20:45:22.530 回答