Moving App.xaml Styles to Resource Dictionary

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?


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"

    <!--Application Resources-->
                <ResourceDictionary Source="Controls/CustomControl.xaml" />

If your control is defined within a class library the Themes/Generic.xaml should look like this

<ResourceDictionary xmlns="" 
        <ResourceDictionary Source="/My.Custom.Assembly;component/FolderLocationOfXaml/CustomControl.xaml" />

No matter where your custom control is placed the xaml for this will always look the same

<ResourceDictionary xmlns=""
        <Style TargetType="local:CustomControl">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
                <ControlTemplate TargetType="local:CustomControl">
                        <! -- Other stuff here -->

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.

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" />

Need Your Help

Dojo build result in empty cache

build dojo

After making a dojo build, on files where there is a dojo/text I got thing like this: