如何在wpf中更改滚动条拇指的前景?

时间:2011-10-24 11:59:18

标签: wpf

我想在代码中更改Scroll bar thumb的前景。

我已经应用了一种改变我拇指背景的风格,但是,我想改变

运行时的前景图像。这是滚动条的样式代码。

<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 个答案:

答案 0 :(得分: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));