如何在MahApps.Metro中创建自定义口音? [英] How To Create A Custom Accent In MahApps.Metro?

查看:44
本文介绍了如何在MahApps.Metro中创建自定义口音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试替代弹出按钮的默认样式.我试图将所有样式移到自己的ResourceDictionary中,并使用< Style x:Key ="DefaultFlyout" TargetType ="controls:Flyout" BasedOn ="{StaticResource {x:Type controls:Flyout}}">,但它始终会忽略我在 BasedOn 中输入的内容.直接使用 {StaticResource Flyout} 不起作用,因为它是未知的标识符,并且 BasedOn 不支持 DynamicResource .

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.

我的资源字典 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>

以及我想使用弹出按钮的部分,位于< controls:FlyoutsControl>

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>

这是结果:

应该是蓝色弹出窗口,因为我使用了< Setter Property ="Theme" Value ="Accent"/> ,但这是行不通的.

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>

推荐答案

这是怎么回事:

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

即使不使用 Style ,您也可以通过以下方式获得相同的结果:

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

 <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 :发布完整的实施详细信息.

EDIT 2: posting full implementation details.

MainWindow.xaml:

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:

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:

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:

MyViewModel.cs:

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

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

EDIT 3: Note on creating custom Accents:

如果您查看 ThemeManager.cs ,则在现有的颜色和强调之间会有一些硬编码,因此创建新的可能并不那么容易.请参见下面的代码段:

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

编辑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"
};

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

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

App.xaml 资源字典中为 MyCustomAccent.xaml 添加和输入:

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

<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 和新创建的自定义口音:

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

这篇关于如何在MahApps.Metro中创建自定义口音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆