【问题标题】:How to modify the default control themes?如何修改默认控件主题?
【发布时间】:2012-03-16 19:23:51
【问题描述】:

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

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

这是我目前所知道的。 下载默认主题:http://archive.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=wpfsamples&DownloadId=7812

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

从这里开始,我还没想好下一步该做什么。如果只是覆盖模板,那并不太难,但我希望应用程序仍然能够感知主题。

提前致谢

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

更新您的 App.xaml

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

其中 AssemblyWithThemes 是包含带有主题文件的 Themes\ 文件夹的程序集的名称。我相信程序集也需要从您的应用程序中引用。

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

然后,所有其他样式都需要指定 BasedOn 属性来保留主题更改。对于动态创建的窗口,我使用了 new Style(Type) 并阻止了使用覆盖的主题。改成新的Style(Type, Style)后,效果很好。

【问题讨论】:

  • 请将解决方案作为答案发布在底部,而不是将其编辑到您的问题中,以便您将其标记为已回答。

标签: wpf wpf-controls


【解决方案1】:

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

更新您的 App.xaml

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

其中 AssemblyWithThemes 是包含带有主题文件的 Themes\ 文件夹的程序集的名称。我相信程序集也需要从您的应用程序中引用。

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

然后,所有附加样式都需要指定 BasedOn 属性来保留主题更改。对于动态创建的窗口,我使用了 new Style(Type),它阻止了使用被覆盖的主题。改成新的Style(Type, Style)后,效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多