简体   繁体   中英

How To Create A Custom Accent In MahApps.Metro?

I'm trying to override the default style of the flyout. I tried to move all my styles in an own ResourceDictionary and used <Style x:Key="DefaultFlyout" TargetType="controls:Flyout" BasedOn="{StaticResource {x:Type controls:Flyout}}"> , but it always ignores what I enter in BasedOn . Directly using {StaticResource Flyout} doesn't work because it's an unknown identifier, and DynamicResource isn't supported for BasedOn .

My resource dictionary 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>

The App.xaml where it is included:

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

And the part where I want to use the flyout, in MainWindow.xaml inside the <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>

This is the result:

在此处输入图片说明

It should be a blue flyout because i used <Setter Property="Theme" Value="Accent" /> but it doesn't work.

Has anyone an idea why this doesn't work? I don't really like the idea of copying the whole flyout style just to make my changes...


The problem seems to be my custom accent, but I just took the normal one and changed a few colors, this doesn't make any sense....

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

What about this:

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

在此处输入图片说明

You could get the same result even without using a Style by doing:

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

EDIT : Using BasedOn style:

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

Dictionary:

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

EDIT 2 : posting full implementation details.

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

EDIT 3 : Note on creating custom Accents:

If you look into ThemeManager.cs there is some hard-coding between existing colors and accents, so it may not be as straightforward to create new ones as one would wish. See code snippet below:

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

EDIT 4 : Adding a new Accent.

Add MyCustomAccent string to 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"
};

Using one of the existing files as a model, create MyCustomAccent.xaml under Styles/Accents folder together with the other existing files and define your accent colors as you please. For instance:

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

Add and entry for MyCustomAccent.xaml in App.xaml resource dictionary:

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

Add the following code to 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);
}

Recompile both MahApps.Metro and your application. Flyout with your newly created custom Accent:

在此处输入图片说明

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