I have a custom control that uses styles that are in a resource dictionary that is in linked in the app.xaml. If I take the link off and add the link to the page that contains the control it doesn't work. Why is that? Why would my control (a dll) need the styles to be in the app.xaml and not just on the page that the control is contained in?
2 Answers
Why would my control (a dll) need the styles to be in the app.xaml and not just on the page that the control is contained in?
Custom controls need a default style. This default style is set within the constructor. eg:
public CustomControl()
{
DefaultStyleKey = typeof(CustomControl);
}
When this is set it looks within the containing assembly for this style. If the control is within an application, then it looks within the App.xaml. If the control is within a class library, it looks within a file Generic.xaml that must be placed within a folder "Themes". You do not need to place the style within either of these files. You can create a separate file that contains the style and reference it from either App.xaml or Themes/Generic.xaml (based on where the control is defined). To do this you create a MergedDictionary within one of those files. If your control is defined in your application you would do
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!--Application Resources-->
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Controls/CustomControl.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<Application.Resources>
</Application>
If your control is defined within a class library the Themes/Generic.xaml should look like this
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/My.Custom.Assembly;component/FolderLocationOfXaml/CustomControl.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
No matter where your custom control is placed the xaml for this will always look the same
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:My.Custom.Assembly.Controls">
<Style TargetType="local:CustomControl">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomControl">
<Grid>
<! -- Other stuff here -->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Without this default style defined, there is no way to determine what style to override. Once the default style is defined you can change the style within your app or anywhere else the control is being used.

- 12,425
- 1
- 25
- 41
-
Here's the problem. The control's style is already set in the control - it has a generic.xaml and all that with the necessary default styles. Does it not look there first and then to the app.xaml? – Barry Franklin Jun 15 '12 at 17:13
Try moving the style into the control to verify that all of the required references are in place for your control to use items from the dictionary. Make sure that the project containing your UserControl has a reference to the project that contains the resource dictionary. Verify your Source path to the dictionary:
<ResourceDictionary Source="/AssemblyName;component/StylesFolderName/ResourceDictionaryName.xaml" />

- 525
- 1
- 4
- 12
-
1The source path for the dictionary that works in the app.xaml is the same one that I put on the page, the path is correct. The resource dictionary is in the same project as the page that contains the control - I've put it in the same folder and it doesn't make a difference. Everything works fine when the app.xaml has the resource dictionary link in it, when I move that link to the page, it fails- the control can't seem to find the styles it needs. All the files in are the same project (the control is a dll)... why does it need to find the style in the app.xaml? – Barry Franklin Jun 15 '12 at 15:45
-
If the control is in a DLL, then the project containing the control will need to reference the project containing the resource dictionary. – jrockers Jun 15 '12 at 17:06
-
The project containing the control IS the project containing the resource dictionary. – Barry Franklin Jun 15 '12 at 17:18