我已经开始在我的WPF应用程序中引入 MahApps.Metro(真的很棒),而且我最喜欢的按钮是默认的。问题是,它把所有的文本都变成大写,而我不想要。
我已经开始在我的WPF应用程序中引入 MahApps.Metro(真的很棒),而且我最喜欢的按钮是默认的。问题是,它把所有的文本都变成大写,而我不想要。
您可以通过在Window.Resources
中设置所有按钮的属性来覆盖默认值。
<controls:MetroWindow
...
xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type Button}"
BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="controls:ButtonHelper.PreserveTextCase" Value="True"/>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid>
<!-- This would have normally made the text uppercase if not for the style override -->
<Button Content="Button"/>
</Grid>
</controls:MetroWindow>
省略 x:Key
设置会导致该样式应用于此 MetroWindow
中的所有按钮。<Application
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">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type Button}"
BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="controls:ButtonHelper.PreserveTextCase" Value="True"/>
</Style>
</ResourceDictionary>
</Application.Resources>
MahApps 2.4.7 更新
<Application
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">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type Button}"
BasedOn="{StaticResource MahApps.Styles.Button}">
<Setter Property="controls:ControlsHelper.ContentCharacterCasing" Value="True"/>
</Style>
</ResourceDictionary>
</Application.Resources>
<Setter Property="controls:ControlsHelper.ContentCharacterCasing" Value="Normal"/>
。 - Mohamed Boulmers<Setter Property="mah:ControlsHelper.ContentCharacterCasing" Value="Normal"/>
我必须使用名称空间“mah”而不是“controls”,以匹配当前快速入门指南的说明。从 XML 片段中使其工作有点棘手,因为它们需要与代码中的其他指令保持一致。下面是一个完整的工作示例。
App.xml:
<Application x:Class="Example.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Example"
xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<!-- Theme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Dark.Blue.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Change default Button character case from Upper to Normal -->
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource MahApps.Styles.Button}">
<Setter Property="mah:ControlsHelper.ContentCharacterCasing" Value="Normal"/>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
MainWindow.xml:
<mah:MetroWindow x:Class="Example.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Example"
mc:Ignorable="d"
xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
TitleCharacterCasing="Normal"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="92,73,0,0" VerticalAlignment="Top" Height="144" Width="197" FontSize="22" />
</Grid>
</mah:MetroWindow>
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
。当然,你需要在项目中安装mahapps包。 - heltonbiker