Changing XAML style dynamically in Code Behind so that controls applying that style also reflect the change

c#, styles, wpf

Solution

You need to use `DynamicResource` so that it can be changed at run-time. You also need to replace the style with a new one, not try to modify the existing one. This works:

<StackPanel>
    <Rectangle Style="{DynamicResource key1}" Height="200" Width="200" x:Name="rect1"/>
    <Button Click="Button_Click" Content="Click"/>
</StackPanel>

Style style = new Style {TargetType = typeof(Rectangle)};
style.Setters.Add(new Setter(Shape.FillProperty, Brushes.Red));
style.Setters.Add(new Setter(UIElement.VisibilityProperty, Visibility.Collapsed));

Application.Current.Resources["key1"] = style;

Problem

I want to be able to set style properties (and values) from the .cs file in my WPF window. My problem is if I have 30 rectangles, all of which I want to have the same style (and I don't want to update all of them individually). I'd like to have them all set (in the xaml file) to the same style, and then update the style to look the way I'd like. Say I set the `Style = "key1"` in the Xaml for each rectangle. Then I want to be able to modify "key1" later so all the rectangles will reflect that change. I tried in `App.xaml` ``` <Application.Resources> <Style x:Key="key1" TargetType="Rectangle"> <Setter Property="Fill" Value="Red"/> </Style> </Application.Resources> ``` In MainwWindows.xaml ``` <StackPanel> <Rectangle Style="{StaticResource key1}" Height="200" Width="200" x:Name="rect1"/> <Button Click="Button_Click" Content="Click"/> </StackPanel> ``` In code behind ``` private void Button_Click(object sender, RoutedEventArgs e) { Style style = Application.Current.Resources["key1"] as Style; style.Setters.Add(new Setter(Rectangle.VisibilityProperty, Visibility.Collapsed)); } ``` This updates the style but do not update the rectangles. Is this possible? Does anyone know how to do this? (An example would be greatly appreciated).

Original source