将自定义样式添加到 Mahapps.Metro 个现有样式
Adding custom styles to Mahapps.Metro existing ones
我用MahApps.Metro。在设置指南中,它告诉您将一些代码包含到 App.xaml 中。所以我做到了。
现在我希望能够向其中添加我自己的样式。例如,这包括所有 windows 默认情况下有边框。
但这不起作用。不应用边框。我知道在不使用 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>
您忘记继承样式 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}" />
希望对您有所帮助!
我最后是这样做的:
不易出错,对懒惰更友好
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
ThemeManager.ChangeAppStyle(this,
ThemeManager.GetAccent("Amber"),
ThemeManager.GetAppTheme("BaseDark"));
var allTypes = typeof(App).Assembly.GetTypes();
var filteredTypes = allTypes.Where(d =>
typeof(MetroWindow).IsAssignableFrom(d)
&& typeof(MetroWindow) != d
&& !d.IsAbstract).ToList();
foreach (var type in filteredTypes)
{
var defaultStyle = this.Resources["MetroWindowDefault"];
this.Resources.Add(type, defaultStyle);
}
base.OnStartup(e);
}
}
我用MahApps.Metro。在设置指南中,它告诉您将一些代码包含到 App.xaml 中。所以我做到了。
现在我希望能够向其中添加我自己的样式。例如,这包括所有 windows 默认情况下有边框。
但这不起作用。不应用边框。我知道在不使用 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>
您忘记继承样式 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}" />
希望对您有所帮助!
我最后是这样做的:
不易出错,对懒惰更友好
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
ThemeManager.ChangeAppStyle(this,
ThemeManager.GetAccent("Amber"),
ThemeManager.GetAppTheme("BaseDark"));
var allTypes = typeof(App).Assembly.GetTypes();
var filteredTypes = allTypes.Where(d =>
typeof(MetroWindow).IsAssignableFrom(d)
&& typeof(MetroWindow) != d
&& !d.IsAbstract).ToList();
foreach (var type in filteredTypes)
{
var defaultStyle = this.Resources["MetroWindowDefault"];
this.Resources.Add(type, defaultStyle);
}
base.OnStartup(e);
}
}