3

当不再需要拇指时,我无法弄清楚如何更改 ScrollBar Opactity。

换句话说:我想拥有像 Auto 一样的 ScrollViewer 的属性 VerticalScrollBarVisibility,但 ScrollViewer 的内容应该保留它的大小。我必须考虑到必须将 ScrollBar 的样式应用于具有内部 ScrollViewers 的 3rd 方控件。

这是我的代码。我试图对 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>

4

1 回答 1

1

我做的 :)

诀窍是在 ControlTemplate 触发器中使用 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>
于 2012-06-23T14:49:47.860 回答