views:

309

answers:

1

Hi,

I have a WPF user control defined in an application assembly. I'm trying to style the user control based on styles in another (skin or theme) assembly. I don't want the theme/skin assembly which contains the resourcedictionary with all the styles to reference the application assembly. Is there any way of doing this? E.g.

WPFApplication Assembly

MyUserControl.xaml <- WPF user control

Theme Assembly

MyStyles.xaml <- WPF resource dictionary

I assume I can use

<Style x:Key="MyStyle">
    <Setter Property="Background" Value="Red" />
</Style>

in the MyStyles.xaml resourcedictionary, and use:

<l:MyUserControl Name="control" Style="{StaticResource MyStyle}" />

in any window that uses MyUserControl.xaml in the WPFApplication assembly

However, I would prefer to target the custom user control in the MyStyles.xaml resourcedictionary:

<Style TargetType="{x:Type l:MyUserControl}" x:Key="MyStyle">
        <Setter Property="Background" Value="Red" />
    </Style>

Is this possible??

+1  A: 

Yes you can do it. Check this SO post where i show how to do it programmatically. I was doing it with ControlTemplates, but the exact same thing applies to Styles as well.

To do it declaratively may be simpler, you may have to programmatically merge your resourcedictionaries, declare the right StaticResources and namespaces in your xaml, and then just reference the style/template that you want.

slugster