wpf MahApps Metro 设置来自应用程序资源的窗口边框样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24336583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:58:16  来源:igfitidea点击:

MahApps Metro setting Window Border styles from App Resources

wpfmahapps.metro

提问by user3102770

I am trying to set Window Border styles for my MahApps Metro app. I have read the articles about how to set the different Border styles and I think I get it. However, I am trying to set the Border Style for all windows in my app to be the same (all Drop Shadow) and it doesn't seem to want to work.

我正在尝试为我的 MahApps Metro 应用程序设置窗口边框样式。我已经阅读了有关如何设置不同边框样式的文章,我想我明白了。但是,我正在尝试将应用程序中所有窗口的边框样式设置为相同(全部为投影),但它似乎不起作用。

I have app resources that look like this:

我有看起来像这样的应用程序资源:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/GSDXThemes;component/GSDXDarkYellowTheme.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

My Resource dictionary looks like this:

我的资源字典如下所示:

<!-- Merge in ResourceDictionaries defining base styles to use. This theme is based on the Metro Dark Yellow theme. -->
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Yellow.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />
    <ResourceDictionary Source="pack://application:,,,/GSDXThemes;component/GSDXControlStyles.xaml" />
</ResourceDictionary.MergedDictionaries>

The GSDXControlStyles dictionary just sets some custom style values for my app. It is in this file that I try to set the Window Borders.

GSDXControlStyles 字典只是为我的应用程序设置了一些自定义样式值。我正是在这个文件中尝试设置窗口边框。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:resx="clr-namespace:GSDXThemes.Properties"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:System="clr-namespace:System;assembly=mscorlib"
                xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                xmlns:GSDUserControls="clr-namespace:GSD.CommonGUI.UserControls;assembly=CommonGUI">

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

<!-- Now customize the theme for our use...mostly just changing font sizes, etc...-->
<Style TargetType="{x:Type Controls:MetroWindow}" >
    <Setter Property="WindowTransitionsEnabled" Value="False" />
    <Setter Property="EnableDWMDropShadow" Value="True" />
</Style>

<Style TargetType="{x:Type Label}" BasedOn="{StaticResource MetroLabel}">
    <Setter Property="FontSize" Value="16"/>
</Style>

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource MetroTextBox}">
    <Setter Property="FontSize" Value="16"/>
</Style>
...

All the other style settings work fine. But the first line for setting the Window Border does nothing. All my windows show with no border.

所有其他样式设置都可以正常工作。但是设置窗口边框的第一行没有任何作用。我所有的窗口都显示无边框。

How can I get this to work so all Windows have the Drop Shadow border?

我怎样才能让它工作以便所有 Windows 都有投影边框?

回答by punker76

you must give your style a key to get a working solution

你必须给你的风格一把钥匙才能得到一个有效的解决方案

<Style x:Key="CustomDefaultWindowStyle"
       TargetType="{x:Type Controls:MetroWindow}"
       BasedOn="{StaticResource {x:Type Controls:MetroWindow}}" >
    <Setter Property="WindowTransitionsEnabled" Value="False" />
    <Setter Property="EnableDWMDropShadow" Value="True" />
</Style>

now use this style on all your MetroWindows

现在在你所有的MetroWindows上使用这种风格

<Controls:MetroWindow x:Class="YourWindowClass"
                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                      xmlns:System="clr-namespace:System;assembly=mscorlib"
                      Title="Custom Window Style Demo"
                      Height="600"
                      Width="800"
                      WindowStartupLocation="CenterScreen"
                      Style="{DynamicResource CustomDefaultWindowStyle}">
...
</Controls:MetroWindow>

(don't be afraid of the'Invalid style target type:...' message, it's a VS bug)

(不要害怕“无效的样式目标类型:...”消息,这是一个 VS 错误)

hope that helps

希望有帮助