0

I want to modify the default control templates for my project. For my first change, I just want to establish a part name for the listview's ScrollViewer in each theme.

Here is my structure: MyControlLibrary: defines a couple controls and defines the themes for the standard wpf controls. MyApplication: uses the control library and included themes.

Here is what I have figured out so far. Download the default themes from: http://archive.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=wpfsamples&DownloadId=7812

Added the downloaded themes to the Themes folder in MyControlLibrary, added necessary references, and modified xmlns:theme namespace to reference the assembly.

From here, I haven't figured out what to do next. If just overriding the template, that is not too hard, but I want the application to still be theme aware.

Thanks in advance

EDIT: Okay, I found the solution for this. I hope this helps someone else.

Update your App.xaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="{ThemeDictionary AssemblyName=AssemblyWithThemes}"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    <!-- other styles go here -->
</Application.Resources>

where AssemblyWithThemes is the name of the assembly that contains the Themes\ folder with the theme files. I believe the assembly needs to be referenced from your application as well.

Very important, if including additional resources directly in the App.xaml file, place them where the comment is above, otherwise the containing ResourceDictionary becomes another resource instead of setting the Application.Resources property.

Then, all additional styles need to specify the BasedOn attribute to preserve the theme changes. For dynamically created windows, I used new Style(Type) and it prevented the overridden theme from being used. After changing to new Style(Type, Style), it worked perfectly.

1
  • Please post the solution as an answer at the bottom instead of editing it into your question, so that you can mark it answered.
    – BoltClock
    Mar 19, 2012 at 20:13

1 Answer 1

0

Okay, I found the solution for this. I hope this helps someone else.

Update your App.xaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="{ThemeDictionary AssemblyName=AssemblyWithThemes}"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    <!-- other styles go here -->
</Application.Resources>

where AssemblyWithThemes is the name of the assembly that contains the Themes\ folder with the theme files. I believe the assembly needs to be referenced from your application as well.

Very important, if including additional resources directly in the App.xaml file, place them where the comment is above, otherwise the containing ResourceDictionary becomes another resource instead of setting the Application.Resources property.

Then, all additional styles need to specify the BasedOn attribute to preserve the theme changes. For dynamically created windows, I used new Style(Type) and it prevented the overridden theme from being used. After changing to new Style(Type, Style), it worked perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.