2011-10-24 97 views
0

我想在我的代碼中更改滾動條大拇指的前景。如何更改wpf中滾動條的縮略圖的前景?

我申請一個風格,改變了我的拇指背景,但是,我想在運行時改變

前景圖像。這裏是我滾動條的樣式代碼。

<ControlTemplate x:Key="MyScrollBar" TargetType="{x:Type ScrollBar}"> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="178"/> 
       </Grid.ColumnDefinitions> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="12" /> 
        <RowDefinition Height="*"/> 
        <RowDefinition Height="12" /> 
       </Grid.RowDefinitions> 
       <Border Grid.Row="1" CornerRadius="2" BorderThickness="0" > 
        <Border.Background> 
         <ImageBrush ImageSource="/HyperVibe;component/Images/Grey_Slider_Background.png" /> 
        </Border.Background> 
       </Border> 
       <RepeatButton Grid.Row="0" Command="ScrollBar.LineUpCommand" Content=" ^" /> 

       <!--IsDirectionReversed set to true draws a ScrollBar with a 
        Track whose lowest value is at the bottom. 
        The default orientation of a ScrollBar is for the Track 
        values to decrease from top to bottom.--> 
       <Track Grid.Row="1" Name="PART_Track" IsDirectionReversed="true"> 
        <Track.Thumb> 
         <Thumb BorderThickness="1" DataContext="{Binding}" > 
          <Thumb.OpacityMask> 
           <ImageBrush ImageSource="/HyperVibe;component/Images/Green%20Slider.png" /> 
          </Thumb.OpacityMask> 
          <Thumb.Background> 
           <ImageBrush ImageSource="/HyperVibe;component/Images/Green%20Slider.png" /> 
          </Thumb.Background> 
         </Thumb> 
        </Track.Thumb> 
       </Track> 
       <RepeatButton Grid.Row="2" Command="ScrollBar.LineDownCommand" Content=" v" /> 
      </Grid> 
     </ControlTemplate> 

任何幫助將不勝感激。

最好的問候, 〜阿努普

回答

1

嘗試,如果你可以加載上面的控制模板作爲DynamicResource做在C#代碼如下。如果您將它作爲靜態資源加載,那麼您將無法編輯資源,因爲它將被密封。

ControlTemplate myCtrlTpl = (ControlTemplate) FindResource("MyScrollBar"); 
Trigger tgrIsMouseOver = new Trigger { Property = Thumb.IsMouseOverProperty, Value = true }; 
Trigger tgrIsMouseNotOver = new Trigger { Property = Thumb.IsMouseOverProperty, Value = false }; 

ImageBrush mImgBrhDefault = (Create what ever brush either imagebrush or solidcolorbrush) 
ImageBrush mImgBrhHighlight = (Create what ever brush either imagebrush or solidcolorbrush) 

tgrIsMouseOver.Setters.Add(new Setter(Thumb.BackgroundProperty, mImgBrhHighlight)); 
tgrIsMouseNotOver.Setters.Add(new Setter(Thumb.BackgroundProperty, mImgBrhDefault));