Mahapps.metro: Why am I going nuts, and why doesn't the Watermark show?

Created on 13 Jun 2016  路  5Comments  路  Source: MahApps/MahApps.Metro

What steps will reproduce this issue?

Been staring at this for a while, but I still can't figure out what I'm doing wrong.

Repo steps:

Create new wpf application. Save it immediately. Throw a TextBox into the grid. Run it and verify all is working.

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

Use nuget to add a package reference to mahapps.metro 1.2.4. Add the mahapps namespace, and add a watermark to the textbox

<Window x:Class="DafuqWatermark.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:DafuqWatermark"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Controls:TextBoxHelper.Watermark="WHAT THE HELL" />
    </Grid>
</Window>

Run it and observe no watermark nowhere on no textbox.

Expected outcome

I just want my promised watermark :(

Am I forgetting something? Why wouldn't it work? I see no errors anywhere...

Environment

  • MahApps.Metro 1.2.4
  • Windows 10
  • Visual Studio 2016
  • .NET Framework 4.5.2

Most helpful comment

did you add resources in app.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" />
                <!--  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.MergedDictionaries>
        </ResourceDictionary>
</Application.Resources>

All 5 comments

did you add resources in app.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" />
                <!--  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.MergedDictionaries>
        </ResourceDictionary>
</Application.Resources>

@AndrzejKl That works for my crappy repo. Unfortunately, I have them in the application where it still isn't working.

Just to be sure I switched mine out for yours, but still no luck.

I even tried placing the merged resources into the user control where the watermark is failing to work. It's not like I'm doing anything crazy--main window>grid>content control, then a data template with my user control in it. The user control only has a grid with some textboxes and buttons in it.

<TextBox Grid.Row="1" Grid.ColumnSpan="4" Text="{Binding Name}" Controls:TextBoxHelper.Watermark="Name" />

Nothing.

@AndrzejKl Got it.

In the resources of my user control, I'm setting the style on all the textboxes.

<UserControl.Resources>
        <Style TargetType="TextBox">
            <Setter Property="FontSize" Value="24" />
        </Style>
 </UserControl.Resources>

That's breaking the watermark.

Is there a base style I can extend to not break the watermark?

Add BasedOn:

<UserControl.Resources>
        <Style TargetType="TextBox" 
               BasedOn="{StaticResource {x:Type TextBox}}">
            <Setter Property="FontSize" 
                    Value="24" />
        </Style>
 </UserControl.Resources>

@AndrzejKl brilliant! Thanks.

Was this page helpful?
0 / 5 - 0 ratings