views:

206

answers:

2

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.

+2  A: 

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.

ChrisF
Typo: Should be StaticResource.
Daniel Rose
@Daniel - cheers, I was retyping rather than pasting.
ChrisF
Well that's what I tried and made Visual Studio crash. Guess it's time for a update.EDIT: I actually just did<Color x:Key="LightColor">LightBlue</Color>So the question is can I use the solidBrush in every property that has a color.
Ingó Vals
@Ingó - as I said I haven't had chance to double check the exact syntax you'll need for WPF. What version of Visual Studio are you using?
ChrisF
I'm using VS2008 with SP1 and .NET framework 3.5 running on Windows Vista. It works now but it sometimes crashes when I'm changing the reference from example color1 to color2 (resource keys of the brushes )
Ingó Vals
@Ingó - I have the same but running on XP installed here, if I get the chance I'll test it out and report back.
ChrisF
I'm getting the feeling that it has nothing to do with the color but rather the fact I'm working in the ControlTemplate, seems to be very volatile.Guess there is no other way to change trigger behaviour then to change the ControlTemplate.
Ingó Vals
@Ingó - have you got access to Expression Blend? It might be more stable in this area. Expression Blend 3 is 100% compatible with VS2008.
ChrisF
Ok this is a known problem and is fixed in a hotfix, which also comes with the Silverlight tools so that's why you didn't get it.See here:http://connect.microsoft.com/VisualStudio/feedback/details/407715/visual-studio-2008-sp1-crashes-when-you-change-a-wpf-control-template-in-resource-dictionary-which-is-used-by-a-control-open-in-another-designer-windowand here:http://code.msdn.microsoft.com/KB958017
Ingó Vals
I installed the Silverlight tools and can confirm it now works as intended. Thank you for your help Chris. Can you maybe tell something about dynamically changin the colors at runtime?
Ingó Vals
A: 

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

Daniel Rose
Can I change the resource even if it's in a resource dictionary?
Ingó Vals
Take a look at http://blogs.microsoft.co.il/blogs/davids/archive/2009/05/19/staticresource-and-dynamicresource.aspx
Daniel Rose