假设我有一个包含许多项目的列表框,因此会出现垂直滚动,但我已经隐藏了滚动条
ScrollViewer.VerticalScrollBarVisibility="Hidden"
有什么办法可以为我添加一个向下滚动的按钮?iv尝试添加
Command="ScrollBar.LineDownCommand"
到一个按钮,但没有任何效果。
您需要告诉 WPF 从哪里开始寻找命令处理程序。不告诉它,它会从 开始查找Button
并且找不到任何处理LineDownCommand
. 不幸的是,将它设置为 是ListBox
不够的,因为它ScrollViewer
是模板的ListBox
一部分,所以 WPF 仍然找不到它。
将其设置为ListBoxItem
s 之一是不正常的,但有效:
<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
在代码隐藏中连接。
我有一个应用程序,我想手动控制 ScrollViewer 的滚动。基本上,我得到了 ScrollViewer 的引用,然后使用 ScrollToHorizontalOffset() 方法来控制滚动。以下是我解释我使用的过程的博客文章:
http://www.developingfor.net/wpf/fun-with-the-wpf-scrollviewer.html
http://www.developingfor.net/wpf/more-fun-with-wpf-scrollviewer.html