创建可滚动的WPF用户控件

use*_*533 1 wpf custom-controls scrollviewer

对于我正在处理的应用程序,我想创建一个自定义控件,上面有几个按钮,如果有太多按钮我需要它来滚动.但是,我不想使用标准滚动条而只是想在控件的两端有两个按钮向上滚动而另一个向下滚动.实现这一目标的最佳方法是什么?

是否可以更改滚动条的样式,使它们只是按钮而不是完整的水平GUI?

Phi*_*hil 21

您通常会使用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>
Run Code Online (Sandbox Code Playgroud)

将在需要时自动显示垂直滚动条.

您可以通过像这样指定VerticalScrollbarVisibility来更改行为

<ScrollViewer VerticalScrollBarVisibility="Auto">
<ScrollViewer VerticalScrollBarVisibility="Visible">
<ScrollViewer VerticalScrollBarVisibility="Hidden">
<ScrollViewer VerticalScrollBarVisibility="Disabled">
Run Code Online (Sandbox Code Playgroud)

当然还有Horizo​​ntalScrollBarVisibility属性.