1

Is there a way to set a global style that would update targeted controls' styles on change, without assigning dynamic resource to every control?

App resources containing the style definition

<Style x:Key="ThemableLabel" TargetType="{x:Type Label}"
    <Setter Property="Foreground" Value="White"/>
</Style>

And then changing the style during runtime

Style style = new Style {
    TargetType = typeof( Label )
};

style.Setters.Add( new Setter( Label.ForegroundProperty, brush ) );
Application.Current.Resources[ "ThemableLabel" ] = style;

requires all the labels to have assigned dynamic resource set to ThemableLabel.

pikausp
  • 1,142
  • 11
  • 31

2 Answers2

2

EDIT
Try this

    <App.Resources>
           <SolidColorBrush Color="CadetBlue" x:Key="Color"/>
    </App.Resources>
        <Style x:Key="ThemableLabel" TargetType="{x:Type Label}"
            <Setter Property="Foreground" Value="{DynamicResource Color}"/>
        </Style>

and change in code property Color of SolidColorBrush.

Resources["Color"] = new SolidColorBrush(Colors.Red);

change will be reflected in any object that has this style assigned. If you use StaticResource no change will occur. Now there is no need to create the whole new style for applying only one diffrent property value.

Maximus
  • 3,458
  • 3
  • 16
  • 27
  • It's not just one property. Also this wouldnt save me from "customizing" every single control, would it? Just instead of assigning DynamicResource to style of the control, i'd assign it's Foreground. – pikausp Aug 19 '14 at 12:02
  • My first answear was not clear, I edited take a look. It is supposed to be working. – Maximus Aug 19 '14 at 12:19
  • Hello max, sorry for the late reply, is this solution supposed to change the ForeGround property of all the labels, with leaving label declaration like ? – pikausp Aug 20 '14 at 11:05
  • To all with assigned style ThemableLabel. You do not have to apply new style when change any of property, just change SolidColorBrush with x:Key="Color", if you want to assign style to all Label do not set Key on style. – Maximus Aug 20 '14 at 11:40
  • Ok, I used the Style declaration that you provided me with and removed the Key property. Now it works like a charm and it udates all the labels that don't require any special assignments ( declaration is enough ), thanks! – pikausp Aug 20 '14 at 12:08
-1

You have to set the 'style' xmal tag to DynamicResource in the all Labels controls:

 <Label Style="{DynamicResource ThemableLabel}"/>
PakKkO
  • 154
  • 5
  • I know that, I was asking if there was a way around? Can I assume from your post that there is not? – pikausp Aug 19 '14 at 09:19
  • Excuseme, I have not seem the last part. – PakKkO Aug 19 '14 at 11:20
  • The DynamicResource is necesary, because StaticResource set the style controls properties during the loading of XAML. More Information http://stackoverflow.com/questions/200839/whats-the-difference-between-staticresource-and-dynamicresource-in-wpf – PakKkO Aug 19 '14 at 11:44