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

时间:2015-03-18 14:21:24

标签: c# wpf mahapps.metro

我使用MahApps.Metro。在设置指南中,它会告诉您在 App.xaml 中包含一些代码。所以我做到了。

现在我希望能够添加我自己的样式。例如,这包括默认情况下具有边框的所有窗口。

但这并不奏效。边框不适用。我知道如何在不使用MahApps.Metro的时候设计样式,但有了它,我就无法兼顾。

这里有什么问题?

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

2 个答案:

答案 0 :(得分:5)

您忘记使用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>

修改

经过测试后,我的第一个答案并不正确。您必须设置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>

使用

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

希望有所帮助!

答案 1 :(得分:1)

我最终这样做了:

较少的错误和更懒惰的

$price = 'SALE PRICE: $' . $discount . 'REG. PRICE: $' . $item->item_cost;

echo $price;