0

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 !

Ben
  • 55
  • 1
  • 6
  • "i need to change, either the value of LPercentBackground1Color and LPercentBackground2Color or where they are used" - can you please clarify what is the objective of changing color?? – ASh Sep 06 '19 at 12:44
  • The objective is to change the color of the bar (this style makes diagonals) LPercentBackground1Color is the bar main color, LPercentBackground2Color the color of the diagonals So i need to change directly their values. Or i need to change `` this – Ben Sep 06 '19 at 12:45
  • if you have some color, predefined by template, and you want to customize them in different vies[s], you can see [this example](https://stackoverflow.com/questions/40657840/set-a-property-of-a-nested-element-in-an-wpf-style) – ASh Sep 06 '19 at 12:52

1 Answers1

1

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;
mm8
  • 163,881
  • 10
  • 57
  • 88
  • I don't know why i though of that earlier ! Thanks that worked and i've been stick on this for quite some time :) – Ben Sep 06 '19 at 12:53