Make "default" text to appear in an empty TextBox without focus using XAML

You could simply use the Extended WPF Toolkit's WatermarkTextBox. enter image description here

I wrote a small guide based on your comment on how you to add and use the library in your project.

Step 1) Right-click on References in your project and choose Add Reference.

Step 1

Step 2) Locate and add the dll file WPFToolkit.Extended.dll.

Step 2

Step 3) Last you need to add the XAML code.

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:extToolkit="http://schemas.xceed.com/wpf/xaml/toolkit"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <extToolkit:WatermarkTextBox Watermark="Enter First Name" />
    </Grid>
</Window>

The key here is to add the reference to the dll file.

xmlns:extToolkit="http://schemas.xceed.com/wpf/xaml/toolkit"

Then you can simply use it in XAML like this.

<extToolkit:WatermarkTextBox Watermark="Enter First Name" />

Whilst there is no real benefit in re-inventing the wheel, it might be interesting to see how this can be done. The easiest way to do this (in pure XAML) is to create a ControlTemplate for the TextBox that overlays a TextBlock when it is not focussed and does not contain text:

<ControlTemplate TargetType="TextBox">
<Grid>
    <TextBox Text="{Binding Text, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}" />
    <TextBlock HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Text="Your Prompt Here"
                Margin="5,0,5,0"
                Foreground="#FF808080"
                FontStyle="Italic"
                IsHitTestVisible="False"
                x:Name="UserMessage"
                Visibility="Hidden"/>
</Grid>
<ControlTemplate.Triggers>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="Text" Value=""/>
            <Condition Property="IsKeyboardFocusWithin" Value="False"/>
            </MultiTrigger.Conditions>
        <Setter Property="Visibility" TargetName="UserMessage" Value="Visible"/>
    </MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>

The MultiTrigger means "set Visibility to Visible if the Text property is empty AND the TextBox does not have keyboard focus"

If you want to make this more reusable then you could create a custom control with this as it's default template and with a Dependency Property containing the prompt message

Tags:

C#

Wpf

Xaml