c# - 带有 LineUp/LineDown 重复按钮的自定义 WPF DataGrid 滚动条

标签 c# .net wpf xaml

我想制作一个自定义 WPF DataGrid ScrollBar,如下所示:

enter image description here

但直到那一刻,我只能做没有三角形按钮的滚动条:

enter image description here

这是我现在拥有的 XAML 代码:

    <UserControl.Resources>
    <Style x:Key="ScrollThumbs" TargetType="{x:Type Thumb}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Grid x:Name="Grid">
                        <Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" Fill="Transparent" />
                        <Border x:Name="Rectangle1" CornerRadius="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto"  Background="{StaticResource GreenBackColor}" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Tag" Value="Horizontal">
                            <Setter TargetName="Rectangle1" Property="Width" Value="Auto" />
                            <Setter TargetName="Rectangle1" Property="Height" Value="7" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
        <Setter Property="Stylus.IsFlicksEnabled" Value="false" />
        <Setter Property="Foreground" Value="#8C8C8C" />
        <Setter Property="Background" Value="{StaticResource GrayBackColor}" />
        <Setter Property="Width" Value="8" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ScrollBar}">
                    <Grid x:Name="GridRoot" Width="8" Background="{TemplateBinding Background}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="0.00001*" />
                        </Grid.RowDefinitions>

                        <Track x:Name="PART_Track" Grid.Row="0" IsDirectionReversed="true" Focusable="false">
                            <Track.Thumb>
                                <Thumb x:Name="Thumb" Background="{TemplateBinding Foreground}" Style="{DynamicResource ScrollThumbs}" />
                            </Track.Thumb>
                            <Track.IncreaseRepeatButton>
                                <RepeatButton x:Name="PageUp" Command="ScrollBar.PageDownCommand" Opacity="0" Focusable="false" />
                            </Track.IncreaseRepeatButton>
                            <Track.DecreaseRepeatButton>
                                <RepeatButton x:Name="PageDown" Command="ScrollBar.PageUpCommand" Opacity="0" Focusable="false" />
                            </Track.DecreaseRepeatButton>
                        </Track>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger SourceName="Thumb" Property="IsMouseOver" Value="true">
                            <Setter Value="{DynamicResource ButtonSelectBrush}" TargetName="Thumb" Property="Background" />
                        </Trigger>
                        <Trigger SourceName="Thumb" Property="IsDragging" Value="true">
                            <Setter Value="{DynamicResource DarkBrush}" TargetName="Thumb" Property="Background" />
                        </Trigger>

                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="Thumb" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                        <Trigger Property="Orientation" Value="Horizontal">
                            <Setter TargetName="GridRoot" Property="LayoutTransform">
                                <Setter.Value>
                                    <RotateTransform Angle="-90" />
                                </Setter.Value>
                            </Setter>
                            <Setter TargetName="PART_Track" Property="LayoutTransform">
                                <Setter.Value>
                                    <RotateTransform Angle="-90" />
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Width" Value="Auto" />
                            <Setter Property="Height" Value="8" />
                            <Setter TargetName="Thumb" Property="Tag" Value="Horizontal" />
                            <Setter TargetName="PageDown" Property="Command" Value="ScrollBar.PageLeftCommand" />
                            <Setter TargetName="PageUp" Property="Command" Value="ScrollBar.PageRightCommand" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ScrollBarPageButton" TargetType="{x:Type RepeatButton}">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <Polygon Points="0,0 0,6, 0,10" Stroke="Black" Fill="Black" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

如何将这些三角形添加为 RepeatButtons?

最佳答案

只需要自己做这件事,然后在搜索时找到你的帖子。您需要将 ScrollBar.LineUpCommand 和 LineDownCommand 按钮(轨道上方和下方)的 RepeatButtons 添加到您的 ScrollBar ControlTemplate,如下所示:

<ControlTemplate TargetType="{x:Type ScrollBar}">
<Grid SnapsToDevicePixels="true">
    <Grid.RowDefinitions>
        <RowDefinition MaxHeight="18"/>
        <RowDefinition Height="0.00001*"/>
        <RowDefinition MaxHeight="18"/>
    </Grid.RowDefinitions>

    <RepeatButton Grid.Row="0" Height="18" Command="{x:Static ScrollBar.LineUpCommand}" />
    <Track x:Name="PART_Track" Grid.Row="1" IsDirectionReversed="true" IsEnabled="{TemplateBinding IsMouseOver}">
        <Track.DecreaseRepeatButton>
            <RepeatButton Command="{x:Static ScrollBar.PageUpCommand}" Style="..."/>
        </Track.DecreaseRepeatButton>
        <Track.IncreaseRepeatButton>
            <RepeatButton Command="{x:Static ScrollBar.PageDownCommand}" Style="..."/>
        </Track.IncreaseRepeatButton>
        <Track.Thumb>
            <Thumb Style="..." Width="14"/>
        </Track.Thumb>
    </Track>
    <RepeatButton Grid.Row="2" Height="18" Command="{x:Static ScrollBar.LineDownCommand}" />
</Grid>

Sacha 在这里有一个完整的样本 link .

关于c# - 带有 LineUp/LineDown 重复按钮的自定义 WPF DataGrid 滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23606810/

相关文章:

c# - ItemStyle-VerticalAlign ="Middle"在 asp :GridView 中不起作用

c# - 单元测试基于计时器的应用程序?

wpf - 在MahApp应用程序中设置图标颜色

.net - WPF应用程序在启动时失败,并显示TypeInitializationException

wpf - XAML 绑定(bind) BitmapImage ViewModel 属性

c# - .Net 与网站管理员工具(Google.Apis.Webmasters.v3 Nuget 包)斗争

c# - 使用 MVC3 创建和编辑字符串集合

c# - 如何通过键从 .NET IGrouping 返回值?

c# - 将 8BIM 配置文件元数据添加到 tiff 图像文件

c# - 反序列化 JSON 的更好方法?