2012-06-21 70 views
2

當我不再需要它的拇指時,我無法弄清楚如何更改ScrollBar Opactity。基於Thumb可見性的WPF ScrollBar不透明度

換句話說:我想讓ScrollViewer的屬性VerticalScrollBarVisibility像Auto,但ScrollViewer的內容應該保留它的大小。我必須考慮到ScrollBar的風格必須應用於帶有內置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> 

回答

0

我做到了:)

訣竅是在控件模板觸發使用SOURCENAME。

現在控件模板的滾動條是這樣的:

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