Ben
Ben

Reputation: 55

How to change a color in a XAML style file

I have a ProgressBar with a defined style and I need to change his color (which is defined in the style) at runtime.

I've been trying looking for like an hour, but nothing was helping my problem

Here is my style (ProgressBar.xaml), i need to change, either the value of LPercentBackground1Color and LPercentBackground2Color or where they are used

    <SolidColorBrush x:Key="LPercentBackground1Color" Color="Red" />
    <SolidColorBrush x:Key="LPercentBackground2Color" Color="White" />

    <Style x:Key="NormalStyle" TargetType="{x:Type ProgressBar}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ProgressBar}">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Determinate" />
                                <VisualState x:Name="Indeterminate" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="PART_Track" Margin="0" BorderThickness="0" CornerRadius="7" />
                        <Border x:Name="PART_Indicator" Margin="0" BorderThickness="0" CornerRadius="5" HorizontalAlignment="Left"
                                    Background="{StaticResource LPercentBackground1Color}" ClipToBounds="True">
                            <Border x:Name="DiagonalDecorator" Width="5000">
                                <Border.Background>
                                    <DrawingBrush TileMode="Tile" Stretch="None" Viewbox="0,0,1,1" Viewport="0,0,36,34" ViewportUnits="Absolute">
                                        <DrawingBrush.RelativeTransform>
                                            <TranslateTransform X="0" Y="0" />
                                        </DrawingBrush.RelativeTransform>
                                        <DrawingBrush.Drawing>
                                            <GeometryDrawing Brush="{StaticResource LPercentBackground2Color}" Geometry="M0,0 -18,0 -36,34 -18,34 Z" />
                                        </DrawingBrush.Drawing>
                                    </DrawingBrush>
                                </Border.Background>
                            </Border>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

I can't find how to change those, so i need your help. Thanks !

Upvotes: 0

Views: 481

Answers (1)

mm8
mm8

Reputation: 169410

If you use the DynamicResource markup extension in your template:

Background="{DynamicResource LPercentBackground1Color}"

...you can replace the Brush in the ResourceDictionary at runtime:

Resources["LPercentBackground1Color"] = Brushes.Green;

Upvotes: 1

Related Questions