Adding custom styles to Mahapps.Metro existing ones

本秂侑毒 提交于 2021-02-08 05:36:16

问题


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>

回答1:


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!




回答2:


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);
    }
}


来源:https://stackoverflow.com/questions/29124602/adding-custom-styles-to-mahapps-metro-existing-ones

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!