There are a WPF User Control library and two (or more) User Controls in it. I need to use the same style in both user controls. How can I share this style? For example:
This is the style:
<Style x:Key="customLabelStyle" TargetType="Label">
...
</Style>
User control A:
<UserControl x:Class="Edu.Wpf.Example.UserControlA"
...xmlns stuff... >
<Grid>
... some xaml markup...
<Label Style="{StaticResource customLabelStyle}"/>
</Grid>
</UserControl>
UserControl B:
<UserControl x:Class="Edu.Wpf.Example.UserControlB"
...xmlns stuff... >
<Grid>
... some another xaml markup...
<Label Style="{StaticResource customLabelStyle}"/>
</Grid>
</UserControl>
So how can I share this style between user controls in the library without involving of the application app.xaml resource dictionary?
UPDATE
I can add Themes\Generic.xaml into my library and define the style there. But in this case I have to use ComponentResourceKey as the key of the style. Right? It's long and not very handy expression...
You can define the shared resources in a separate
ResourceDictionary
, then merge them into yourUserControl
's Resources using MergedDictionaries.I found the solution that works in design time too (at least in VS2010) :
This attached property can be applied to a FrameworkElement. Imagine the customLabelStyle is defined in the Styles.xaml dictionary in the Edu.Wpf.Example project. So this style can be applied in the next way:
Say that you have one resource defining colors, like this:
And another one defining some basic styles like this:
You can then add your resources to the App.xaml's Application.Resources tag as shown here:
Then, in all your UserControls, you can use the styles or brushes as StaticResources as your example code shows.