简体   繁体   English

如何使用MahApps.Metro在所有WPF窗口中应用相同的主题

[英]How to apply the same theme in all WPF windows using MahApps.Metro

I'm newer with WPF projects and I have a question about the component MahApps.Metro. 我是WPF项目的新手,我对组件MahApps.Metro有疑问。 I created a MainWindow and other windows in my project, my question is: How to apply the same theme in MainWindow to the others windows of the project? 我在项目中创建了一个MainWindow和其他窗口,我的问题是:如何将MainWindow中的相同主题应用到项目的其他窗口? And, I must to apply the ResourceDictionary for all windows or exist any way to do this one time only? 并且,我必须为所有窗口应用ResourceDictionary,或者只存在一次这样做吗?

Thank you guys! 感谢大伙们!

In your App.xaml , set the ResourceDictionary property like this: App.xaml ,像这样设置ResourceDictionary属性:

<Application.Resources>
    <ResourceDictionary Source="MyResourceDictionary.xaml"/>
</Application.Resources>

Notice that in order for the styles in your resource dictionary to be applied to all controls of that type, the style should NOT have a key . 请注意,为了将资源字典中的样式应用于该类型的所有控件,该样式不应具有键 For example, a style defined like this: 例如,一个定义如下的样式:

<Style TargetType="{x:Type Button}">
    <Setter Property="Content" Value="This is the default button text"/>
</Style>

In the ResourceDictionary will cause all the buttons in the project to have a default value when you create them. ResourceDictionary将导致项目中的所有按钮在创建时具有默认值。

Resource's in WPF work based on their available "scope". WPF中的资源基于其可用的“范围”工作。 Check this article for a detailed explanation 请查看此文章以获取详细说明

Now Application (App.xaml) is in a higher scope to Window . 现在Application (App.xaml)在Window的更高范围内。 Thus to share these MahApps control resources across your Window 's just move the ResourceDictionary 's to the Application.Resources scope 因此,要在Window共享这些MahApps控制资源,只需将ResourceDictionary移动到Application.Resources范围即可。

something like: 就像是:

<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
      <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

Now you no longer need to add these Dictionary's in each Window as they are available implicitly 现在,您不再需要在每个Window中添加这些Dictionary,因为它们是隐式可用的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM