0

I have a button that i want to have a dynamic color based on if a file exists.

I am using two resources to do this, one resource is a color and one is a SolidColorBrush. They are declared as so

<Window.Resources>
<Color x:Key="ColorName">Red</Color>
<SolidColorBrush x:Key="ButtonColor1" Color="{StaticResource ColorName}" />
</Window.Resources>

Now, my button is done in a strange way as i wanted to change the highlight color, its done as so

Button Grid.Row="1" Grid.Column="2" Height="25" Width="25" 
HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Click" 
Name="SalutButton">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Background" Value="{StaticResource 
ButtonColor1}"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <Border Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="1">
                                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="LightGreen"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>

My code to try and change the resource is:

Resources["ColorName"] = System.Windows.Media.Colors.Green;

This successfully changes the resource colorname, but the buttoncolor1 resource is already made during initialisation and takes the default color of colorname which is red, so even though im changing the resource its not updating.

Any ideas?

Ben Bodie
  • 75
  • 12
  • change StaticResource -> DynamicResource – ASh May 22 '18 at 15:12
  • Hero ASh, thanks very much pal! :) – Ben Bodie May 22 '18 at 15:16
  • Possible duplicate of [What's the difference between StaticResource and DynamicResource in WPF?](https://stackoverflow.com/questions/200839/whats-the-difference-between-staticresource-and-dynamicresource-in-wpf) – ASh May 22 '18 at 15:22

0 Answers0