Take the 2-minute tour ×

I don't think this has been asked before, if so please redirect me. I'm new to WPF, and I've tried everything I could think of with no success, I'm stuck.

I'm using a WPF Theme, and I want to add some custom styles I created to it. For example, all TextBlock are supposed to have Red foreground, but I have a group of TextBlock that I want in Blue.

So far I've been doing this in the xaml, creating a <Style></Style> in the resources, and calling it using Style="{StaticResource StyleName}". But I want to add it to the theme files instead, and I don't know how to give it a name and call it from the xaml.

I guess I'd start with something like this, but how do I link both elements?

In the theme file (MyStyles.xaml or TextEdit.xaml or similar):

<Style TargetType="{x:Type TextBlock}" x:Key="KeyName" ???>

And then in my xaml:

<TextBlock Name="TextBlockName"

I need this style to be in the Theme because the program will allow users to change themes, and these styles can't hardcoded be in the xaml.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You want to first merge that resource file into your resources :

            <ResourceDictionary Source="MyStyles.xaml" />
        <dxc:IntToBooleanConverter x:Key="IntToBooleanConverter" />

and then you can use it with

<TextBlock Name="TextBlockName" Style="{StaticResource KeyName}" />
share|improve this answer
Would the file change automatically if I swap the theme? –  Yisela Feb 12 '13 at 21:24
@yisela Yes, that's what I use to keep my styles central and make sure my application looks consistently the same between windows. –  JBG Feb 12 '13 at 21:31
add comment

If you have loaded your Theme file you can access any of the Styles/Resources the same way as local Styles/Resources

If you use Style="{StaticResource StyleName}" it will look first in the Window/UserControl, if not found it will look though the loaded Resource dictionaries. so as long as you have loaded the Theme (Resource Dictionary) it will work fine.

share|improve this answer
add comment

Your Answer


