3

我尝试在 WPF 中自定义ScrollBara的外观和感觉,只需在. 这种风格改变了新的风格。几乎工作正常,除了我不能按住鼠标并拖动滚动,我只需点击(两者都可以)滚动。我还是 WPF 的新手,我不知道这里可能存在什么问题。这是代码(有点长,但请不要介意,我猜你对此很熟悉):DataGridScrollBarTemplateScrollBarThumbRepeatButtonsLine buttonsPage buttons

<DataGrid HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding}" SnapsToDevicePixels="True"> 
        <DataGrid.Resources>                
            <Style TargetType="ScrollBar">                    
                <Style.Resources>
                    <ControlTemplate x:Key="verRepeat" TargetType="RepeatButton">
                        <Border CornerRadius="4" BorderBrush="Green" BorderThickness="1" Background="Yellow">
                            <ContentPresenter Width="18" Height="18"/>
                        </Border>
                    </ControlTemplate>
                    <ControlTemplate x:Key="midRepeat" TargetType="RepeatButton">
                        <Border Background="Transparent">
                            <ContentPresenter/>
                        </Border>
                    </ControlTemplate>

                    <ControlTemplate x:Key="verScroll" TargetType="ScrollBar">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition MaxHeight="18"/>
                                <RowDefinition/>
                                <RowDefinition MaxHeight="18"/>
                            </Grid.RowDefinitions>
                            <Border Grid.RowSpan="3"/>
                            <RepeatButton Grid.Row="0" Width="18" Command="ScrollBar.LineUpCommand" Template="{StaticResource verRepeat}">                                    

                            </RepeatButton>
                            <Track Grid.Row="1" IsDirectionReversed="True">
                                <Track.DecreaseRepeatButton>
                                    <RepeatButton Command="ScrollBar.PageUpCommand" Template="{StaticResource midRepeat}">

                                    </RepeatButton>
                                </Track.DecreaseRepeatButton>
                                <Track.Thumb>
                                    <Thumb Margin="2,0,2,0">
                                        <Thumb.Template>
                                            <ControlTemplate TargetType="Thumb">
                                                <Border Background="Green"/>                                                    
                                            </ControlTemplate>
                                        </Thumb.Template>
                                    </Thumb>
                                </Track.Thumb>
                                <Track.IncreaseRepeatButton>
                                    <RepeatButton Command="ScrollBar.PageDownCommand" Template="{StaticResource midRepeat}">

                                    </RepeatButton>
                                </Track.IncreaseRepeatButton>
                            </Track>
                            <RepeatButton Grid.Row="2" Command="ScrollBar.LineDownCommand" Template="{StaticResource verRepeat}">

                            </RepeatButton>
                        </Grid>
                    </ControlTemplate>

                    <ControlTemplate x:Key="horScroll" TargetType="ScrollBar">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition MaxWidth="18"/>
                                <ColumnDefinition Width="0.00001*"/>
                                <ColumnDefinition MaxWidth="18"/>
                            </Grid.ColumnDefinitions>
                            <Border Grid.ColumnSpan="3"/>
                            <RepeatButton Grid.Column="0" Height="18" Command="ScrollBar.LineLeftCommand" Template="{StaticResource verRepeat}">

                            </RepeatButton>
                            <Track Grid.Column="1" IsDirectionReversed="False" Focusable="False">
                                <Track.DecreaseRepeatButton>
                                    <RepeatButton Command="ScrollBar.PageLeftCommand" Template="{StaticResource midRepeat}">

                                    </RepeatButton>
                                </Track.DecreaseRepeatButton>
                                <Track.Thumb>
                                    <Thumb>
                                        <Thumb.Template>
                                            <ControlTemplate TargetType="Thumb">
                                                <Grid>
                                                    <Border Background="Green" Margin="0,1,0,1"/>
                                                    <ContentPresenter Width="18" Height="18"/>
                                                </Grid>
                                            </ControlTemplate>
                                        </Thumb.Template>
                                    </Thumb>
                                </Track.Thumb>
                                <Track.IncreaseRepeatButton>
                                    <RepeatButton Command="ScrollBar.PageRightCommand" Template="{StaticResource midRepeat}">

                                    </RepeatButton>
                                </Track.IncreaseRepeatButton>
                            </Track>
                            <RepeatButton Grid.Column="2" Command="ScrollBar.LineRightCommand" Template="{StaticResource verRepeat}">

                            </RepeatButton>
                        </Grid>
                    </ControlTemplate>
                </Style.Resources>
                <Setter Property="OverridesDefaultStyle" Value="True"/>                        
                <Style.Triggers>
                    <Trigger Property="Orientation" Value="Horizontal">
                        <Setter Property="Template" Value="{StaticResource horScroll}"/>
                    </Trigger>
                    <Trigger Property="Orientation" Value="Vertical">
                        <Setter Property="Template" Value="{StaticResource verScroll}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>                
        </DataGrid.Resources>
    </DataGrid>

我想我在这里错过了一些东西,Thumb似乎无法与之互动。您的帮助将不胜感激。谢谢!

这是网格的屏幕截图:

在此处输入图像描述

4

1 回答 1

2

一切都很好,只需Track添加PART_Track如下名称:

<Track x:Name="PART_Track" Grid.Row="1" IsDirectionReversed="True" ... />

没有这个Thumb是行不通的。

注意:在设计模板和样式时,最好查看原件,至少要知道模式的名称。

这里是关于Template.

于 2013-07-27T19:32:23.483 回答