基于 Thumb 可见性的 WPF 滚动条不透明度



当不再需要它的拇指时,我不知道如何更改滚动条操作。

换句话说:我希望像Auto一样拥有ScrollViewer的属性VerticalScrollBarVisibility,但ScrollViewer的内容应该保留其大小。我必须考虑到滚动条的样式必须应用于具有内部滚动查看器的第三方控件。

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

我做到了:)

诀窍是在控件模板触发器中使用源名称。

现在滚动条的控件模板如下所示:

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

相关内容

  • 没有找到相关文章

最新更新