Question

In WPF I have a few resource dictionaries and in them styles for my panels and controls in my app. I'm reusing the same colors again and again. I actually have 5 colors and they give my app a good color-scheme.

However if I wan't to change the theme I have to go into the RD's and change each and every color there.

I would like to somewhere have the colors set but don't know how or where. I tried to put a color tag in one RD but as soon as I referenced it in the same RD Visual Studio crashed.

Also the best solution would be that I could have the color as a dynamic setting in the app itself so users could even change it themselves.

Was it helpful?

Solution

The following code works in Silverlight, so should work in WPF (possibly with some modification - I haven't had time to double check it):

In your Resources define your colours:

<SolidColorBrush x:Key="MyNamedColor" Color="DarkRed"/>

Then define some styles:

<Style x:Key="MyTextBlockStyle" TargetType="TextBlock">
    <Setter Property="Foreground" Value="{StaticResource MyNamedColor}"/>
</Style>
<Style x:Key="MyLineStyle" TargetType="Line">
    <Setter Property="Stroke" Value="{StaticResource MyNamedColor}"/>
</Style>

Then in your code (either in the XAML or in the code behind) use these styles on all your TextBlocks, Lines etc.

Then when you want to change the colours just update the original SolidColorBrush definition.

OTHER TIPS

Besides ChrisF's solution: If you want it to be dynamic at runtime, you could use DynamicResource and change the resource itself at runtime.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top