MahApps - 如何禁用默认按钮的自动大写

12

我已经开始在我的WPF应用程序中引入 MahApps.Metro(真的很棒),而且我最喜欢的按钮是默认的。问题是,它把所有的文本都变成大写,而我不想要。

4个回答

20

您可以通过在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 中的所有按钮。

3
我觉得MahApps.Metro默认不应该使用大写字母,但感谢您提供的解决方案。它非常好用。 - mababin
1
你也可以使用 "http" mahapps 命名空间: xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"。当然,你需要在项目中安装mahapps包。 - heltonbiker
如何保留所有内容的原始大小写?“正常大小写”不应该是正常情况吗?! - Leon
1
ButtonHelper.PreserveTextCase现在被替换为ControlsHelper.ContentCharacterCasing。 - Kux

7
如果您将ImaBrokeDude的答案应用到您的App.xaml中,它将适用于项目中任何窗口中的所有按钮。
<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>


1
在设计器中看起来很好,但在运行时,按钮样式回退到默认的WPF样式。 - tm1
ButtonHelper.PreserveTextCase现在被ControlsHelper.ContentCharacterCasing所取代。 我发现BasedOn = "{StaticResource MahApps.Styles.Button}"似乎可以工作。 - Kux

5

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>

2
我认为应该是<Setter Property="controls:ControlsHelper.ContentCharacterCasing" Value="Normal"/> - Mohamed Boulmers

0
以下代码对我有效: <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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接