How to modify the default control themes?

给你一囗甜甜゛ 提交于 2019-12-25 08:14:00

问题


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:


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.



来源:https://stackoverflow.com/questions/9743330/how-to-modify-the-default-control-themes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!