Dynamically changing the XAML style in Code Behind, so controls using this style also reflect the change

I want to be able to set style properties (and values) from a .cs file in my WPF window.

My problem is that I have 30 rectangles, all of which I want to have the same style (and I don't want to update them all separately). I would like all of them to be installed (in the xaml file) in the same style, and then updated the style to look as I would like.

Say I set Style = "key1" in Xaml for each rectangle. Then I want to be able to modify "key1" later, so all the rectangles reflect this 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

 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 does not update the rectangles.

Is it possible? Does anyone know how to do this? (An example would be greatly appreciated).

+8
source share
3 answers

You need to use DynamicResource so that it can be changed at runtime. You also need to replace the style with a new one, and not try to change the existing one. It 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; 
+12
source

It is also worth noting that styles are sealed after use and therefore cannot be changed. This is why styles should be replaced with a different instance rather than an updated one.

+3
source

Several static helpers created, usage example:

 SetStyle(typeof(ContentPage), (ContentPage.BackgroundColorProperty, Color.Green), (ContentPage.PaddingProperty, new Thickness(20))); 

Helper Methods:

  public static Style CreateStyle(Type target, params (BindableProperty property, object value)[] setters) { Style style = new Style(target); style.ApplyToDerivedTypes = true; foreach (var setter in setters) { style.Setters.Add(new Setter { Property = setter.property, Value = setter.value }); } return style; } public static void SetStyle(Type target, params (BindableProperty property, object value)[] setters) { Style style = new Style(target); style.ApplyToDerivedTypes = true; foreach (var setter in setters) { style.Setters.Add(new Setter { Property = setter.property, Value = setter.value }); } Application.Current.Resources.Add(style); } 
0
source

All Articles