在 Mahapps.Metro 中设置 WindowButtonCommands 样式
Setting WindowButtonCommands styles in Mahapps.Metro
我想更改我的 WPF 应用程序的“最小值”、“最大值”和“关闭”按钮的样式。
我正在使用 Mahapps.Metro 并且我已经成功地实现了我想要的结果,但只使用了过时的 WindowMinButtonStyle
、WindowMaxButtonStyle
和 WindowCloseButtonStyle
属性MetroWindow
class。例如 WindowMinButtonStyle
属性 上的过时消息为:
This property will be deleted in the next release. You should use LightMinButtonStyle or DarkMinButtonStyle in WindowButtonCommands to override the style.
问题是我不知道具体怎么做。 MetroWindow
class 有一个名为 WindowButtonCommands
的字段,但它是 internal
,所以这似乎是错误的树。我是 WPF 的新手,他们网站上的指南中没有关于如何执行此操作的信息,所以我很迷茫。我希望有人能为我提供一个简短的代码示例,为我指明正确的方向。
EDIT - 这是产生警告的 XAML:
<controls:MetroWindow x:Class="Project.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"
WindowMinButtonStyle="{DynamicResource DarkWindowButtonStyle}"
WindowMaxButtonStyle="{DynamicResource DarkWindowButtonStyle}"
WindowCloseButtonStyle="{DynamicResource DarkWindowCloseButtonStyle}">
<Grid>
</Grid>
</controls:MetroWindow>
我还应该提一下,我使用的是 Mahapps.Metro 的新 v1.2.0,但我与以前的版本有同样的问题。
Mahapps.Metro 具有 Obsolete
属性的源代码:https://github.com/MahApps/MahApps.Metro/blob/develop/MahApps.Metro/Controls/MetroWindow.cs#L88-L93
现在有两种样式(浅色、深色)将基于 OverrideDefaultWindowCommandsBrush
属性(在 MetroWindow
可用)和亮度(默认为浅色样式)使用.
所以,把这些放在你的App.xaml
(或其他地方)
<Style x:Key="CustomLightMetroWindowButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource LightMetroWindowButtonStyle}">
<Setter Property="Foreground" Value="Chocolate" />
</Style>
<Style x:Key="CustomDarkMetroWindowButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource DarkMetroWindowButtonStyle}">
<Setter Property="Foreground" Value="Crimson" />
</Style>
<Style TargetType="{x:Type controls:WindowButtonCommands}" BasedOn="{StaticResource {x:Type controls:WindowButtonCommands}}">
<Setter Property="LightMinButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="LightMaxButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="LightCloseButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="DarkMinButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
<Setter Property="DarkMaxButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
<Setter Property="DarkCloseButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
</Style>
编辑
如果您只想在一个 window 处使用它,那么您可以创建一个带有键的样式并在 window 处使用它,如下所示:
<controls:MetroWindow.WindowButtonCommands>
<controls:WindowButtonCommands Style="{DynamicResource CustomWindowButtonCommandsStyleLocatedtInAppXaml}" />
</controls:MetroWindow.WindowButtonCommands>
样式位于App.xaml
<Style x:Key="CustomWindowButtonCommandsStyleLocatedtInAppXaml" TargetType="{x:Type controls:WindowButtonCommands}" BasedOn="{StaticResource {x:Type controls:WindowButtonCommands}}">
<Setter Property="LightMinButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="LightMaxButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="LightCloseButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="DarkMinButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
<Setter Property="DarkMaxButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
<Setter Property="DarkCloseButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
</Style>
希望对您有所帮助。
我想更改我的 WPF 应用程序的“最小值”、“最大值”和“关闭”按钮的样式。
我正在使用 Mahapps.Metro 并且我已经成功地实现了我想要的结果,但只使用了过时的 WindowMinButtonStyle
、WindowMaxButtonStyle
和 WindowCloseButtonStyle
属性MetroWindow
class。例如 WindowMinButtonStyle
属性 上的过时消息为:
This property will be deleted in the next release. You should use LightMinButtonStyle or DarkMinButtonStyle in WindowButtonCommands to override the style.
问题是我不知道具体怎么做。 MetroWindow
class 有一个名为 WindowButtonCommands
的字段,但它是 internal
,所以这似乎是错误的树。我是 WPF 的新手,他们网站上的指南中没有关于如何执行此操作的信息,所以我很迷茫。我希望有人能为我提供一个简短的代码示例,为我指明正确的方向。
EDIT - 这是产生警告的 XAML:
<controls:MetroWindow x:Class="Project.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"
WindowMinButtonStyle="{DynamicResource DarkWindowButtonStyle}"
WindowMaxButtonStyle="{DynamicResource DarkWindowButtonStyle}"
WindowCloseButtonStyle="{DynamicResource DarkWindowCloseButtonStyle}">
<Grid>
</Grid>
</controls:MetroWindow>
我还应该提一下,我使用的是 Mahapps.Metro 的新 v1.2.0,但我与以前的版本有同样的问题。
Mahapps.Metro 具有 Obsolete
属性的源代码:https://github.com/MahApps/MahApps.Metro/blob/develop/MahApps.Metro/Controls/MetroWindow.cs#L88-L93
现在有两种样式(浅色、深色)将基于 OverrideDefaultWindowCommandsBrush
属性(在 MetroWindow
可用)和亮度(默认为浅色样式)使用.
所以,把这些放在你的App.xaml
(或其他地方)
<Style x:Key="CustomLightMetroWindowButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource LightMetroWindowButtonStyle}">
<Setter Property="Foreground" Value="Chocolate" />
</Style>
<Style x:Key="CustomDarkMetroWindowButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource DarkMetroWindowButtonStyle}">
<Setter Property="Foreground" Value="Crimson" />
</Style>
<Style TargetType="{x:Type controls:WindowButtonCommands}" BasedOn="{StaticResource {x:Type controls:WindowButtonCommands}}">
<Setter Property="LightMinButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="LightMaxButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="LightCloseButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="DarkMinButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
<Setter Property="DarkMaxButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
<Setter Property="DarkCloseButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
</Style>
编辑
如果您只想在一个 window 处使用它,那么您可以创建一个带有键的样式并在 window 处使用它,如下所示:
<controls:MetroWindow.WindowButtonCommands>
<controls:WindowButtonCommands Style="{DynamicResource CustomWindowButtonCommandsStyleLocatedtInAppXaml}" />
</controls:MetroWindow.WindowButtonCommands>
样式位于App.xaml
<Style x:Key="CustomWindowButtonCommandsStyleLocatedtInAppXaml" TargetType="{x:Type controls:WindowButtonCommands}" BasedOn="{StaticResource {x:Type controls:WindowButtonCommands}}">
<Setter Property="LightMinButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="LightMaxButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="LightCloseButtonStyle" Value="{StaticResource CustomLightMetroWindowButtonStyle}" />
<Setter Property="DarkMinButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
<Setter Property="DarkMaxButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
<Setter Property="DarkCloseButtonStyle" Value="{StaticResource CustomDarkMetroWindowButtonStyle}" />
</Style>
希望对您有所帮助。