user3102770
user3102770

Reputation: 21

MahApps Metro setting Window Border styles from App Resources

I am trying to set Window Border styles for my MahApps Metro app. I have read the articles about how to set the different Border styles and I think I get it. However, I am trying to set the Border Style for all windows in my app to be the same (all Drop Shadow) and it doesn't seem to want to work.

I have app resources that look like this:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/GSDXThemes;component/GSDXDarkYellowTheme.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

My Resource dictionary looks like this:

<!-- Merge in ResourceDictionaries defining base styles to use. This theme is based on the Metro Dark Yellow theme. -->
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.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/Yellow.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />
    <ResourceDictionary Source="pack://application:,,,/GSDXThemes;component/GSDXControlStyles.xaml" />
</ResourceDictionary.MergedDictionaries>

The GSDXControlStyles dictionary just sets some custom style values for my app. It is in this file that I try to set the Window Borders.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:resx="clr-namespace:GSDXThemes.Properties"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:System="clr-namespace:System;assembly=mscorlib"
                xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                xmlns:GSDUserControls="clr-namespace:GSD.CommonGUI.UserControls;assembly=CommonGUI">

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
</ResourceDictionary.MergedDictionaries>

<!-- Now customize the theme for our use...mostly just changing font sizes, etc...-->
<Style TargetType="{x:Type Controls:MetroWindow}" >
    <Setter Property="WindowTransitionsEnabled" Value="False" />
    <Setter Property="EnableDWMDropShadow" Value="True" />
</Style>

<Style TargetType="{x:Type Label}" BasedOn="{StaticResource MetroLabel}">
    <Setter Property="FontSize" Value="16"/>
</Style>

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource MetroTextBox}">
    <Setter Property="FontSize" Value="16"/>
</Style>
...

All the other style settings work fine. But the first line for setting the Window Border does nothing. All my windows show with no border.

How can I get this to work so all Windows have the Drop Shadow border?

Upvotes: 2

Views: 6749

Answers (1)

punker76
punker76

Reputation: 14611

you must give your style a key to get a working solution

<Style x:Key="CustomDefaultWindowStyle"
       TargetType="{x:Type Controls:MetroWindow}"
       BasedOn="{StaticResource {x:Type Controls:MetroWindow}}" >
    <Setter Property="WindowTransitionsEnabled" Value="False" />
    <Setter Property="EnableDWMDropShadow" Value="True" />
</Style>

now use this style on all your MetroWindows

<Controls:MetroWindow x:Class="YourWindowClass"
                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                      xmlns:System="clr-namespace:System;assembly=mscorlib"
                      Title="Custom Window Style Demo"
                      Height="600"
                      Width="800"
                      WindowStartupLocation="CenterScreen"
                      Style="{DynamicResource CustomDefaultWindowStyle}">
...
</Controls:MetroWindow>

(don't be afraid of the'Invalid style target type:...' message, it's a VS bug)

hope that helps

Upvotes: 3

Related Questions