Frage

I've tried customizing the look and feel of the ScrollBar of a DataGrid in WPF, simply by applying new style on the ScrollBar. This style changes the Template of the ScrollBar to a new one. Almost works fine except that I can't hold mouse down on the Thumb and drag to scroll, I just can click on the RepeatButtons (both Line buttons and Page buttons work OK) to scroll.

I'm still new to WPF, I don't know what the problem might be here. Here is the code:

<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>

I think I miss something here, the Thumb seems to be unable to interact with.

Here is the screenshot of the grid:

enter image description here

War es hilfreich?

Lösung

All is fine, just to Track to add the name of PART_Track as follows:

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

Without this Thumb does not work.

Note: When designing your template and style better look at the original, at least in order to know the names of the pattern.

Here is a little description about the importance parts of the Template.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top