简体   繁体   中英

Adding custom styles to Mahapps.Metro existing ones

I use MahApps.Metro . In the guide to setting it up, it tells you to include some code into the App.xaml . So I did.

Now I wanted to be able to add my own styles to it. This for instance includes all windows to have a border by default.

But that doesn't work. Borders are not applied. I know how to style stuff when not using MahApps.Metro, but with it, I can't get both working.

What's wrong here?

<Application x:Class="ProjectName.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="Windows/MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
                <!-- This is what I added -->
                <ResourceDictionary xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro">
                    <Style TargetType="Controls:MetroWindow">
                        <Setter Property="BorderThickness" Value="1" />
                        <Setter Property="BorderBrush" Value="{DynamicResource AccentColorBrush}" />
                    </Style>
                </ResourceDictionary>
                <!-------------------------->
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

You have forgotten to inherit the style with BasedOn :

<ResourceDictionary xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro">
    <Style TargetType="Controls:MetroWindow"
           BasedOn="{StaticResource {x:Type Controls:MetroWindow}}">
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="BorderBrush" Value="{DynamicResource AccentColorBrush}" />
    </Style>
</ResourceDictionary>

EDIT

after tested it out, my first answer is not really correct. you must set a x:Key and use this key in every MetroWindow xaml.

<Style x:Key="CustomGlobalMetroWindow"
        TargetType="{x:Type Controls:MetroWindow}"
        BasedOn="{StaticResource {x:Type Controls:MetroWindow}}">
    <Setter Property="BorderThickness"
            Value="1" />
    <Setter Property="BorderBrush"
            Value="Purple" />
</Style>

usage

<Controls:MetroWindow x:Class="Demo"
                      Style="{DynamicResource CustomGlobalMetroWindow}" />

Hope that helps!

I ended up doing it this way:

Less errorprone and more laziness-friendly

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        ThemeManager.ChangeAppStyle(this,
                                    ThemeManager.GetAccent("Amber"),
                                    ThemeManager.GetAppTheme("BaseDark"));

        var allTypes = typeof(App).Assembly.GetTypes();
        var filteredTypes = allTypes.Where(d =>
            typeof(MetroWindow).IsAssignableFrom(d)
            && typeof(MetroWindow) != d
            && !d.IsAbstract).ToList();

        foreach (var type in filteredTypes)
        {
            var defaultStyle = this.Resources["MetroWindowDefault"];
            this.Resources.Add(type, defaultStyle);
        }

        base.OnStartup(e);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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