WPF的Windows 7主题?

42

有没有办法让一个在XP上运行的WPF应用程序看起来像是在Windows 7上运行?我正在寻找一种可以直接粘贴的主题。我知道Codeplex上的主题项目(https://archive.codeplex.com/?p=wpfthemes),但它不支持DataGrid,而这正是我急需的。我想也许Windows 7主题只是一个简单的移植,或者已经存在于某个文件中。


更新

借鉴@Lars Truijens的想法,我成功地为主要控件实现了Windows 7外观,但不幸的是,这种方法并不能适用于我需要使用的WPF Toolkit DataGrid控件。

DataGrid在Aero主题下的外观如下所示

Windows XP-look DataGrid

DataGrid应该看起来像这样

Windows 7-look DataGrid

所以,如果有人有任何想法,我仍在寻找解决这个问题的方法。也许有人已经构建了一个扩展Aero主题,覆盖了WPF工具包控件?再次感谢您提供的任何信息。


更新2 - DataGrid问题已解决!

要使Aero主题与DataGrid或任何其他WPF Toolkit控件配合使用,您只需要添加第二个Aero字典,因此您的App.xaml现在应该如下所示。

<Application.Resources>
    ...
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary
                Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml" />
            <ResourceDictionary
                Source="pack://application:,,,/WPFToolkit;component/Themes/Aero.NormalColor.xaml" />
            ...
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

另外,我建议在您的DataGrid控件中关闭网格线(因为它们看起来很糟糕):

<DataGrid GridLinesVisibility="None" ...>

1
你的更新2救了我的命!!!感谢您发布该更新! - Sonosar
3个回答

56

WPF自带所有Windows版本的标准主题。例如,您可以通过以下步骤在Windows XP上使用Aero主题(Vista和Windows 7使用):

  1. 将PresentationFramework.Aero添加到应用程序的引用列表中,作为一个依赖项
  2. 编辑您的App.xaml

从这里开始

<Application.Resources>
  <!-- Your stuff here -->
</Application.Resources>

变成这样

<Application.Resources>
  <ResourceDictionary>
    <!-- Put your stuff here instead -->

    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources> 

来源: http://mrpmorris.blogspot.com/2008/05/using-vista-aero-theme-in-xp-wpf-apps.html

以下为其他替代方案。请确保将相应的程序集添加到您的应用程序引用列表中作为要求。

<ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Classic;component/themes/Classic.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Royale;component/themes/Royale.NormalColor.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Luna.Homestead;component/themes/Luna.Homestead.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Luna.Metallic;component/themes/Luna.Metallic.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Zune;component/themes/Zune.NormalColor.xaml"/>

我终于有机会试用了。它确实可以工作,但正如我担心的那样,对于“DataGrid”控件则不行。请查看我的更新。具有Aero主题的“DataGrid”的外观仍然是XP风格的。 - devuxer
太好了,我刚刚偶然发现了解决“DataGrid”问题的方法:<ResourceDictionary Source="pack://application:,,,/WPFToolkit;component/Themes/Aero.NormalColor.xaml" />。我也会更新我的问题。 - devuxer
3
你需要将动态引用转换为完整引用,否则你需要部署PresentationFramework.aero。在这里查看我的答案:https://dev59.com/0msz5IYBdhLWcg3wADIm#8185946 - Helge Klein
它给了我一个“FileNotFoundException: Could not load file or assembly 'PresentationFramework.Aero”。 - mrid

4

在 Lars 的回答和 DanM 的更新中有一个补充:

在部署时,你必须将 Aero Dll 添加到安装目录中。

你可以通过进入你添加到引用中的 PresentationFramework.Aero 的属性并设置 CopyLocal=True 来实现。 然后,你需要进入任何部署工具(我喜欢 WIX...)并将其添加到部署文件列表中。


1
我认为PresentationFramework.aero不需要部署。根据http://msdn.microsoft.com/en-us/library/ff462634.aspx,它已经包含在.NET框架中了。 - Helge Klein
1
如果使用完整引用,则无需部署PresentationFramework.aero。请参见我的答案:https://dev59.com/0msz5IYBdhLWcg3wADIm#8185946 - Helge Klein

0

前往您的解决方案/项目属性,在“引用”下,您将能够添加对PresentationFramework.Aero的引用... 在您的代码中应用它,它应该可以很好地工作

希望我的回答能帮到您


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接