对于我正在处理的应用程序,我想创建一个自定义控件,上面有几个按钮,如果按钮太多,我需要它滚动。但是,我不想使用标准滚动条,我只想在控件的两端各有两个按钮,一个向上滚动,另一个向下滚动。实现这一目标的最佳方法是什么?
是否可以更改滚动条的样式,使它们只是按钮而不是完整的水平 GUI?
最佳答案
您通常会使用 ScrollViewer 来实现这一点。例如:
<UserControl x:Class="WpfApplication2.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="300">
<ScrollViewer>
<StackPanel>
<Button Content="Test"/>
<Button Content="Test"/>
<Button Content="Test"/>
<Button Content="Test"/>
<Button Content="Test"/>
<Button Content="Test"/>
<Button Content="Test"/>
<Button Content="Test"/>
<Button Content="Test"/>
<Button Content="Test"/>
<Button Content="Test"/>
<Button Content="Test"/>
</StackPanel>
</ScrollViewer>
</UserControl>
将在需要时自动显示垂直滚动条。
您可以像这样指定 VerticalScrollbarVisibility 来更改行为
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ScrollViewer VerticalScrollBarVisibility="Visible">
<ScrollViewer VerticalScrollBarVisibility="Hidden">
<ScrollViewer VerticalScrollBarVisibility="Disabled">
当然还有 HorizontalScrollBarVisibility 属性。
关于wpf - 创建可滚动的 WPF 用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9645974/