wpf - 创建可滚动的 WPF 用户控件

标签 wpf custom-controls scrollviewer

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

是否可以更改滚动条的样式,使它们只是按钮而不是完整的水平 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">

当然还有 Horizo​​ntalScrollBarVisibility 属性。

关于wpf - 创建可滚动的 WPF 用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9645974/

相关文章:

.NET WinForms 自定义控件 : how to get a reference to the containing form

c# - WPF/C# - 将日期格式应用于 ListView

c# - DataTrigger 绑定(bind)到 UserControl 依赖属性

C#自定义控件渲染其他控件

javascript - Highcharts 极坐标图显示特定刻度季度的标签

c# - ScrollViewer 内的 WPF DataGrid

c# - Tablet WPF Windows 桌面应用程序 - 滚动问题

c# - 自定义控件内的控件不可移动

wpf - 是否可以为 StaticResource 动态创建 ResourceKey? (例如使用绑定(bind))

wpf - 限制 Silverlight/WPF 行为可见性