简体   繁体   English

将自定义样式添加到Mahapps.Metro现有样式

[英]Adding custom styles to Mahapps.Metro existing ones

I use MahApps.Metro . 我使用MahApps.Metro In the guide to setting it up, it tells you to include some code into the App.xaml . 在设置指南中,它会告诉您在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. 我知道如何在不使用MahApps.Metro时设置样式,但有了它,我无法兼顾两者。

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 : 您忘记了使用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. 你必须设置一个x:Key并在每个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);
    }
}

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

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