繁体   English   中英

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

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

我是WPF项目的新手,我对组件MahApps.Metro有疑问。 我在项目中创建了一个MainWindow和其他窗口,我的问题是:如何将MainWindow中的相同主题应用到项目的其他窗口? 并且,我必须为所有窗口应用ResourceDictionary,或者只存在一次这样做吗?

感谢大伙们!

App.xaml ,像这样设置ResourceDictionary属性:

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

请注意,为了将资源字典中的样式应用于该类型的所有控件,该样式不应具有键 例如,一个定义如下的样式:

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

ResourceDictionary将导致项目中的所有按钮在创建时具有默认值。

WPF中的资源基于其可用的“范围”工作。 请查看此文章以获取详细说明

现在Application (App.xaml)在Window的更高范围内。 因此,要在Window共享这些MahApps控制资源,只需将ResourceDictionary移动到Application.Resources范围即可。

就像是:

<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>

现在,您不再需要在每个Window中添加这些Dictionary,因为它们是隐式可用的

暂无
暂无

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

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