如何在MahApps.Metro中创建自定义重音?

时间:2016-03-22 06:38:04

标签: c# wpf xaml mahapps.metro

我试图覆盖弹出窗口的默认样式。我试图在自己的ResourceDictionary中移动我的所有样式并使用<Style x:Key="DefaultFlyout" TargetType="controls:Flyout" BasedOn="{StaticResource {x:Type controls:Flyout}}">,但它总是忽略我在BasedOn中输入的内容。直接使用{StaticResource Flyout}无效,因为它是未知标识符,DynamicResource不支持BasedOn

我的资源字典Controls.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls">

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

        ... <!-- other custom resourceDictionaries -->

        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedSingleRowTabControl.xaml" />
    </ResourceDictionary.MergedDictionaries>
    ...
    <Style x:Key="DefaultFlyout" TargetType="controls:Flyout" BasedOn="{StaticResource {x:Type controls:Flyout}}">
        <Setter Property="Theme" Value="Accent" />
    </Style>
</ResourceDictionary>

包含它的App.xaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MyApplication;component/View/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedSingleRowTabControl.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:,,,/MyApplication;component/View/CustomAccent.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/baselight.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

我要在MainWindow.xaml

内的<controls:FlyoutsControl>中使用弹出窗口的部分
<controls:FlyoutsControl.ItemContainerStyle>
    <Style BasedOn="{StaticResource DefaultFlyout}"
       TargetType="{x:Type controls:Flyout}">
        <Setter Property="Header"
            Value="{Binding Header}" />
        <Setter Property="IsOpen"
            Value="{Binding Visible}" />
        <Setter Property="Position"
            Value="{Binding Position, Converter={StaticResource FlyoutPositionConverter}}" />
        <Setter Property="IsModal"
            Value="{Binding IsModal}" />
    </Style>
</controls:FlyoutsControl.ItemContainerStyle>

结果如下:

enter image description here

它应该是一个蓝色弹出按钮,因为我使用<Setter Property="Theme" Value="Accent" />但它不起作用。

有谁知道为什么这不起作用?我不太喜欢复制整个弹出式样式的想法,只是为了做出改变......

问题似乎是我的自定义口音,但我只是采用了正常的口音并改变了一些颜色,这没有任何意义....

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:options="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                    mc:Ignorable="options">

    <!--ACCENT COLORS-->
    <Color x:Key="HighlightColor">#FF009FDA</Color> <!-- changed -->
    <Color x:Key="AccentColor">#FF009FDA</Color> <!-- changed -->
    <Color x:Key="AccentColor2">#CC009FDA</Color> <!-- changed -->
    <Color x:Key="AccentColor3">#99009FDA</Color> <!-- changed -->
    <Color x:Key="AccentColor4">#66009FDA</Color> <!-- changed -->

    <!-- re-set brushes too -->
    <SolidColorBrush x:Key="HighlightBrush" Color="{StaticResource HighlightColor}" options:Freeze="True" />
    <SolidColorBrush x:Key="AccentColorBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />
    <SolidColorBrush x:Key="AccentColorBrush2" Color="{StaticResource AccentColor2}" options:Freeze="True" />
    <SolidColorBrush x:Key="AccentColorBrush3" Color="{StaticResource AccentColor3}" options:Freeze="True" />
    <SolidColorBrush x:Key="AccentColorBrush4" Color="{StaticResource AccentColor4}" options:Freeze="True" />

    <SolidColorBrush x:Key="WindowTitleColorBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />

    <LinearGradientBrush x:Key="ProgressBrush" EndPoint="0.001,0.5" StartPoint="1.002,0.5" options:Freeze="True">
        <GradientStop Color="{StaticResource HighlightColor}" Offset="0" />
        <GradientStop Color="{StaticResource AccentColor3}" Offset="1" />
    </LinearGradientBrush>

    <SolidColorBrush x:Key="CheckmarkFill" Color="{StaticResource AccentColor}" options:Freeze="True" />
    <SolidColorBrush x:Key="RightArrowFill" Color="{StaticResource AccentColor}" options:Freeze="True" />

    <Color x:Key="IdealForegroundColor">White</Color>
    <SolidColorBrush x:Key="IdealForegroundColorBrush" Color="{StaticResource IdealForegroundColor}" options:Freeze="True" />
    <SolidColorBrush x:Key="IdealForegroundDisabledBrush" Color="{StaticResource IdealForegroundColor}" Opacity="0.4" options:Freeze="True" />
    <SolidColorBrush x:Key="AccentSelectedColorBrush" Color="{StaticResource IdealForegroundColor}" options:Freeze="True" />

    <!-- DataGrid brushes -->
    <SolidColorBrush x:Key="MetroDataGrid.HighlightBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />
    <SolidColorBrush x:Key="MetroDataGrid.HighlightTextBrush" Color="{StaticResource IdealForegroundColor}" options:Freeze="True" />
    <SolidColorBrush x:Key="MetroDataGrid.MouseOverHighlightBrush" Color="{StaticResource AccentColor3}" options:Freeze="True" />
    <SolidColorBrush x:Key="MetroDataGrid.FocusBorderBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />
    <SolidColorBrush x:Key="MetroDataGrid.InactiveSelectionHighlightBrush" Color="{StaticResource AccentColor2}" options:Freeze="True" />
    <SolidColorBrush x:Key="MetroDataGrid.InactiveSelectionHighlightTextBrush" Color="{StaticResource IdealForegroundColor}" options:Freeze="True" />

    <SolidColorBrush x:Key="OverlayBrush" Color="Black" Opacity="0.7"/>  <!-- added -->
</ResourceDictionary>

1 个答案:

答案 0 :(得分:2)

这个怎么样:

<Controls:MetroWindow x:Class="MahApps.Metro.Application5.MainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                  Title="MainWindow"
                  Height="350"
                  Width="525" Loaded="MetroWindow_Loaded">
<Controls:MetroWindow.Resources>
    <Style x:Key="FlyoutStyle1" TargetType="{x:Type Controls:Flyout}">
        <Setter Property="Theme" Value="Accent"></Setter>
    </Style>
</Controls:MetroWindow.Resources>
<Controls:MetroWindow.Flyouts>
    <Controls:FlyoutsControl>
        <Controls:Flyout Header="My Sample Flyout"
                         Position="Right"
                         Style="{DynamicResource FlyoutStyle1}">
            <TextBlock Text="This is a sample flyout."></TextBlock>
        </Controls:Flyout>
    </Controls:FlyoutsControl>
</Controls:MetroWindow.Flyouts>
<Grid>
    <Button Click="Button_Click" RenderTransformOrigin="0.071,0.492" HorizontalAlignment="Left" VerticalAlignment="Center">Flyout Test</Button>
</Grid>

enter image description here

即使不使用Style,您也可以获得相同的结果:

 <Controls:Flyout Header="My Sample Flyout"
                         Position="Right"
                         Theme="Accent">

编辑:使用BasedOn样式:

<Controls:MetroWindow.Flyouts>
    <Controls:FlyoutsControl ItemContainerStyle="{DynamicResource FlyoutStyle2}">
        <Controls:Flyout Position="Right">
            <TextBlock Text="This is a sample flyout."/>
        </Controls:Flyout>
    </Controls:FlyoutsControl>
</Controls:MetroWindow.Flyouts>

字典:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                xmlns:local="clr-namespace:MahApps.Metro.Application5">

<Style x:Key="FlyoutStyle1" TargetType="{x:Type Controls:Flyout}">
    <Setter Property="Theme" Value="Accent"></Setter>
</Style>
<Style x:Key="FlyoutStyle2" TargetType="{x:Type Controls:Flyout}" BasedOn="{StaticResource FlyoutStyle1}">
    <Setter Property="Header" Value="{Binding Header}" />
</Style>
</ResourceDictionary>

编辑2 :发布完整的实施细节。

MainWindow.xaml:

<Controls:MetroWindow
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                  xmlns:local="clr-namespace:MahApps.Metro.Application5" 
                  x:Class="MahApps.Metro.Application5.MainWindow"
                  Title="MainWindow"
                  Height="350"
                  Width="525">
<Controls:MetroWindow.Flyouts>
    <Controls:FlyoutsControl ItemContainerStyle="{DynamicResource FlyoutStyle2}">
        <Controls:Flyout Position="Right">
            <TextBlock Text="This is a sample flyout."/>
        </Controls:Flyout>
    </Controls:FlyoutsControl>
</Controls:MetroWindow.Flyouts>

<Controls:MetroWindow.DataContext>
    <local:MyViewModel/>
</Controls:MetroWindow.DataContext>

<Grid>
    <Button Click="Button_Click" RenderTransformOrigin="0.071,0.492" VerticalAlignment="Center" Content="Flyout Test" HorizontalAlignment="Left"/>
</Grid>

MainWindow.cs:

 public partial class MainWindow : MetroWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var flyout = this.Flyouts.Items[0] as Flyout;

        flyout.IsOpen = !flyout.IsOpen;
    }
}

的App.xaml:

<Application x:Class="MahApps.Metro.Application5.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="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" />
            <ResourceDictionary Source="pack://application:,,,/Dictionary1.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Dictionary1.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                xmlns:local="clr-namespace:MahApps.Metro.Application5">

<Style x:Key="FlyoutStyle1" TargetType="{x:Type Controls:Flyout}">
    <Setter Property="Theme" Value="Accent"></Setter>
</Style>
<Style x:Key="FlyoutStyle2" TargetType="{x:Type Controls:Flyout}" BasedOn="{StaticResource ResourceKey=FlyoutStyle1}">
    <Setter Property="Header" Value="{Binding Header}" />
</Style>

MyViewModel.cs:

public class MyViewModel
{
    public MyViewModel()
    {
        Header = "My Header";
    }
    public string Header { get; set; }
}

编辑3 :关于创建自定义口音的注意事项:

如果你研究ThemeManager.cs现有的颜色和重音之间存在一些硬编码,那么创建新的颜色和重音可能并不像人们希望的那样简单。请参阅下面的代码段:

var colors = new[] {
   "Red", "Green", "Blue", "Purple", "Orange", "Lime", "Emerald", "Teal", "Cyan", "Cobalt",
   "Indigo", "Violet", "Pink", "Magenta", "Crimson", "Amber", "Yellow", "Brown", "Olive", "Steel", "Mauve", "Taupe", "Sienna"
  };

 foreach (var color in colors)
 {
   var resourceAddress = new Uri(string.Format("pack://application:,,,/MahApps.Metro;component/Styles/Accents/{0}.xaml", color));
   _accents.Add(new Accent(color, resourceAddress));
 }

编辑4 :添加新的重音。

MyCustomAccent字符串添加到ThemeManager.cs

var colors = new[] {
    "Red", "Green", "Blue", "Purple", "Orange", "Lime", "Emerald", "Teal", "Cyan", "Cobalt",
     "Indigo", "Violet", "Pink", "Magenta", "Crimson", "Amber", "Yellow", "Brown", "Olive", "Steel", "Mauve", "Taupe", "Sienna", "MyCustomAccent"
};

使用其中一个现有文件作为模型,在MyCustomAccent.xaml文件夹下创建Styles/Accents以及其他现有文件,并根据需要定义您的强调色。例如:

<!--ACCENT COLORS-->
<Color x:Key="HighlightColor">#FF9F00DA</Color>
<!-- changed -->
<Color x:Key="AccentColor">#FF9F00DA</Color>
<!-- changed -->
<Color x:Key="AccentColor2">#CC9F00DA</Color>
<!-- changed -->
<Color x:Key="AccentColor3">#999F00DA</Color>
<!-- changed -->
<Color x:Key="AccentColor4">#669F00DA</Color>
<!-- changed -->

MyCustomAccent.xaml资源字典中添加App.xaml条目:

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

将以下代码添加到MainWindow.cs

private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
{
    var theme = ThemeManager.DetectAppStyle(Application.Current);
    var accent = ThemeManager.GetAccent("MyCustomAccent");
    ThemeManager.ChangeAppStyle(Application.Current, accent, theme.Item1);
}

重新编译MahApps.Metro和您的应用程序。 Flyout使用新创建的自定义重音:

enter image description here