基于Thumb可见性的WPF ScrollBar不透明度

时间:2012-06-21 10:55:02

标签: wpf styles slider scrollbar visibility

我不知道如何在不再需要拇指的情况下更改ScrollBar Opactity。

换句话说:我想让ScrollViewer的属性VerticalScrollBarVisibility像Auto一样,但ScrollViewer的内容应保留其大小。我必须考虑到ScrollBar的样式必须应用于内部ScrollViewers的第三方控件。

这是我的代码。我尝试对Thumb的IsVisible触发器做出反应,但没有成功。

<Style x:Key="ScrollBarThumb" TargetType="{x:Type Thumb}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Thumb}">
            <Border x:Name="ScrollBarThumbBorder" CornerRadius="4" />
        </ControlTemplate>
    </Setter.Value>
</Setter>
<!--<Style.Triggers>
    <Trigger Property="IsVisible" Value="False">
        <Setter Property="Opacity" Value="0.3" />
    </Trigger>
</Style.Triggers>-->

<ControlTemplate x:Key="VerticalScrollBar" TargetType="{x:Type ScrollBar}">
<Border CornerRadius="4">
    <Track
        Name="PART_Track"
        IsDirectionReversed="true">
        <Track.DecreaseRepeatButton>
            <RepeatButton 
                Style="{StaticResource ScrollBarPageButton}"
                Command="ScrollBar.PageUpCommand" />
        </Track.DecreaseRepeatButton>
        <Track.Thumb>
            <Thumb Style="{StaticResource ScrollBarThumb}" />
        </Track.Thumb>
        <Track.IncreaseRepeatButton>
            <RepeatButton 
                Style="{StaticResource ScrollBarPageButton}"
                Command="ScrollBar.PageDownCommand" />
                    </Track.IncreaseRepeatButton>
    </Track>
</Border>

<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Style.Triggers>
    <Trigger Property="Orientation" Value="Vertical">
        <Setter Property="Width" Value="10"/>
        <Setter Property="Height" Value="Auto" />
        <Setter Property="Template" Value="{StaticResource VerticalScrollBar}" />
    </Trigger>
</Style.Triggers>

1 个答案:

答案 0 :(得分:0)

我做到了:)

诀窍是在ControlTemplate Trigger中使用SourceName。

现在,ScrollBar的ControlTemplate如下所示:

    <ControlTemplate x:Key="VerticalScrollBar" TargetType="{x:Type ScrollBar}">
    <Border
        Background="{StaticResource ScrollBarBackgroundBrush}"
        CornerRadius="4">
        <Track
            Name="PART_Track"
            IsDirectionReversed="true">
            <Track.DecreaseRepeatButton>
                <RepeatButton 
                    Style="{StaticResource ScrollBarPageButton}"
                    Command="ScrollBar.PageUpCommand" />
            </Track.DecreaseRepeatButton>
            <Track.Thumb>
                <Thumb Name="Thumb" Style="{StaticResource ScrollBarThumb}" />
            </Track.Thumb>
            <Track.IncreaseRepeatButton>
                <RepeatButton 
                    Style="{StaticResource ScrollBarPageButton}"
                    Command="ScrollBar.PageDownCommand" />
                        </Track.IncreaseRepeatButton>
        </Track>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger SourceName="Thumb" Property="IsVisible" Value="False">
            <Setter Property="Opacity" Value="0" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>