Jerry Nixon on Windows: Super-Simple WPF Watermark

Jerry Nixon on Windows

Tuesday, July 19, 2011

Super-Simple WPF Watermark

I added a watermark to a TextBox just by stacking it on top of a TextBlock and setting its TextBox.Background to Transparent.

All that in so few lines of XAML. I love how XAML can do that.

See it here:

<!--  textbox  (with watermark) -->
<Grid Height="23">
    <TextBlock Text="Type Here to Filter" Foreground="Silver"
        Height="{Binding Height, ElementName=MyTextBox}" 
        VerticalAlignment="Center" Margin="5,0,0,0">
            <Setter Property="TextBlock.Visibility" Value="Hidden"/> 
                <DataTrigger Binding="{Binding Path=Text.Length, 
                    ElementName=MyTextBox}" Value="0"> 
                    <Setter Property="TextBlock.Visibility" Value="Visible"/> 
    <TextBox Background="Transparent" VerticalAlignment="Stretch" 
        VerticalContentAlignment="Center" Name="MyTextBox"