将全局 style/event 应用于 MahApps.Metro 文本框会禁用水印

Applying a global style/event to a MahApps.Metro textbox disables the watermark

我正在开发一个 windows 应用程序并利用来自 NuGet 1.5.0 版的 MahApps.Metro。我 运行 遇到了设置全局属性导致文本框消失的问题。以下是重现步骤:

  1. 创建新的 WPF 应用程序
  2. 从 NuGet(控制台或命令行)安装 MahApps.Metro
  3. 配置 App.xaml 和 MainWindow.xaml(和 .cs)来处理 MahApps.Metro
  4. 创建一个带有水印 属性 的简单文本框(默认为 Controls:TextBoxHelper.Watermark="<watermark here>")。
  5. 运行申请。水印按预期显示。

这是我的示例应用程序的 .xaml。请注意,我将 Controls 更改为 mah(没关系;我尝试了 Controlsmah 没有任何区别):

<mah:MetroWindow x:Class="MahAppsTest.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:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:local="clr-namespace:MahAppsTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox mah:TextBoxHelper.Watermark="Test" Margin="5" VerticalAlignment="Top"/>
    </Grid>
</mah:MetroWindow>

现在,如果我进入 App.xaml 文件并添加以下内容:

<Style TargetType="TextBox">
    <Setter Property="Margin" Value="5"/>
</Style>

从文本框中删除 属性(或保留在那里,没关系),然后 运行 应用程序,水印现在消失了。它是 <Setter> 还是 <EventSetter> 并不重要。不过,水印显示在 IDE 中。

我在他们的 GitHub 回购协议下看到一些人通过将 BasedOn="{StaticResource {x:Type TextBox}}" 添加到 <Style> 标签解决了这个问题,但似乎所有这些都应用于 MainWindow.xaml 或等效项,而不是 App.xaml 文件中的全局项。

编辑: 我还尝试在制作 post.

之前添加 {x:Type ...} 参数

有没有人遇到过这种情况并知道解决方法?

我 运行ning MahApps.Metro v1.5.0 在 Visual Studio 2017 年在 Windows 10 上用 C# v4.6.1(根据目标框架)编程。

您的风格需要以 MahApps 风格为基础。正如所写,您将扩展 default TextBox 样式(没有水印)。试试这个:

<Style TargetType="TextBox"
       BasedOn="{StaticResource MetroTextBox}">
  <Setter Property="Margin" Value="5"/>
</Style>

认为可行。不过,您需要确保 MahApps 样式在您声明该资源时的范围内。如果您将 MahApps 资源拉入您的 App.xaml,那么只需确保进一步向下声明此样式。

如果由于某种原因这不起作用,请尝试将 MetroTextBox 替换为 {x:Type TextBox}。它 looks like MetroTextBox 是样式名称,但我可能错了。

虽然我不确定你的意思:

Configure App.xaml and MainWindow.xaml (and .cs) to handle MahApps.Metro

如果您的意思是要将 MahApps.Metro 样式资源导入到 App.xaml MainWindow.xaml 中,那么这是不必要的。将它们放在 App.xaml 中应该就足够了。


这是我尝试过的,对我有用:

App.xaml:

<Application x:Class="WpfTest.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
  <Application.Resources>
    <ResourceDictionary>

      <!-- MahApps.Metro Resource Imports -->
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/MahApps.Metro;component/Styles/Controls.xaml" />
        <ResourceDictionary Source="/MahApps.Metro;component/Styles/Fonts.xaml" />
        <ResourceDictionary Source="/MahApps.Metro;component/Styles/Colors.xaml" />
        <ResourceDictionary Source="/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
        <ResourceDictionary Source="/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
      </ResourceDictionary.MergedDictionaries>

      <!-- Global TextBox Style Override -->
      <Style TargetType="TextBox" BasedOn="{StaticResource MetroTextBox}">
        <Setter Property="Margin" Value="50" />
        <Setter Property="BorderBrush" Value="Crimson" />
      </Style>

    </ResourceDictionary>
  </Application.Resources>
</Application>

MainWindow.xaml:

<m:MetroWindow x:Class="WpfTest.MainWindow"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:m="http://metro.mahapps.com/winfx/xaml/controls"
               SizeToContent="WidthAndHeight">
  <TextBox m:TextBoxHelper.Watermark="Test" Width="300" />
</m:MetroWindow>

结果:

我在没有 MahApps 的情况下也测试了这个问题并得到了相同的结果(在 App.xaml 中引用的 ResourceDictionary 中的基本样式)。

经过一些尝试和错误后,我找到了一个解决方案,无论是否使用 MahApps。

诀窍是在一个额外的 ResourceDictionary(也可以是一个单独的)中定义一个带有键的基本样式,并使用它来定义一个现在到处都在使用的基本样式。

<Application x:Class="WpfApplication16.App"
             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"
             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" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
                <!-- Accent and AppTheme setting -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />

                <ResourceDictionary>
                    <Style x:Key="BaseTextBoxStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
                        <Setter Property="Background" Value="Goldenrod" />
                        <Setter Property="Margin" Value="5" />
                        <Setter Property="controls:TextBoxHelper.ClearTextButton" Value="True" />
                    </Style>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>

            <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseTextBoxStyle}" />

        </ResourceDictionary>
    </Application.Resources>
</Application>

样本

<Window x:Class="WpfApplication16.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"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">

        <TextBox Margin="10" Text="Test" MinWidth="200" />

    </StackPanel>
</Window>

希望对您有所帮助!