VaioWay
VaioWay

Reputation: 402

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. 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? And, I must to apply the ResourceDictionary for all windows or exist any way to do this one time only?

Thank you guys!

Upvotes: 0

Views: 2294

Answers (2)

Viv
Viv

Reputation: 17380

Resource's in WPF work based on their available "scope". Check this article for a detailed explanation

Now Application(App.xaml) is in a higher scope to Window. Thus to share these MahApps control resources across your Window's just move the ResourceDictionary's to the Application.Resources scope

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

Upvotes: 1

Arian Motamedi
Arian Motamedi

Reputation: 7413

In your App.xaml, set the ResourceDictionary property like this:

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

Upvotes: 1

Related Questions