如何修改默认控件主题? [英] How to modify the default control themes?

查看:23
本文介绍了如何修改默认控件主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改我的项目的默认控件模板.对于我的第一次更改,我只想为每个主题中的列表视图的 ScrollViewer 建立一个部分名称.

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.

这是我的结构:MyControlLibrary:定义几个控件并定义标准 wpf 控件的主题.MyApplication:使用控件库和包含的主题.

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.

这是我目前想到的.从以下位置下载默认主题:http://archive.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=wpfsamples&DownloadId=7812

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

将下载的主题添加到 MyControlLibrary 中的 Themes 文件夹,添加必要的引用,并修改 xmlns:theme 命名空间以引用程序集.

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.

提前致谢

好的,我找到了解决方案.我希望这对其他人有帮助.

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

更新您的 App.xaml

Update your App.xaml

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

其中,AssemblyWithThemes 是包含带有主题文件的 Themes\ 文件夹的程序集的名称.我认为该程序集也需要从您的应用程序中引用.

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.

非常重要,如果直接在 App.xaml 文件中包含其他资源,请将它们放在上面注释的位置,否则包含的 ResourceDictionary 将成为另一个资源,而不是设置 Application.Resources 属性.

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.

然后,所有其他样式都需要指定 BasedOn 属性以保留主题更改.对于动态创建的窗口,我使用了 new Style(Type) 并防止使用覆盖的主题.改成新的样式(类型,样式)后,完美运行.

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.

推荐答案

好的,我找到了解决方案.我希望这对其他人有帮助.

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

更新您的 App.xaml

Update your App.xaml

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

其中,AssemblyWithThemes 是包含带有主题文件的 Themes\ 文件夹的程序集的名称.我认为该程序集也需要从您的应用程序中引用.

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.

非常重要,如果直接在 App.xaml 文件中包含其他资源,请将它们放在上面注释的位置,否则包含的 ResourceDictionary 将成为另一个资源,而不是设置 Application.Resources 属性.

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.

然后,所有其他样式都需要指定 BasedOn 属性以保留主题更改.对于动态创建的窗口,我使用了 new Style(Type) 并防止使用覆盖的主题.改成新的样式(类型,样式)后,完美运行.

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.

这篇关于如何修改默认控件主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆