How to add "type here" message to WPF textbox?
If the text field does not have keyboard focus and does not contain text, then display italicized gray text "enter here" "inside". How? What does not work:
- Setting the property
Text
- this will mess up the data binding. - Template overriding - it's messy and overridden anyway.
It is desirable that the solution be as much XAML and as little C # as possible. Perhaps I could create an attached property that would do the magic?
source to share
What you are looking for is known as a Watermark . I'm not familiar with WPF, but searching for WPF and Watermark on "google" returns tons of results.
source to share
Style your TextBox. This is one of the easiest ways to add a watermark to your TextBox. The code creates a simple style and associates it with the TextBox.
(Code extracted from: http://social.msdn.microsoft.com/Forums/vstudio/en-US/d565c79e-6b4a-44e2-a566-a4a622eab22f/how-to-set-watermark-for-textbox-in-simple -way? forum = wpf )
<Window.Resources>
<Style x:Key="MyWaterMarkStyle" TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<Border Background="White" BorderBrush="#FF7D8683" BorderThickness="1"/>
<ScrollViewer x:Name="PART_ContentHost" Margin="5,0,0,0" VerticalAlignment="Center" />
<Label Margin="5,0,0,0" x:Name="WaterMarkLabel" Content="{TemplateBinding Tag}" VerticalAlignment="Center"
Visibility="Collapsed" Foreground="Gray" FontFamily="Arial"/>
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Text" Value=""/>
</MultiTrigger.Conditions>
<Setter Property="Visibility" TargetName="WaterMarkLabel" Value="Visible"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="DimGray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
...
<TextBox Style="{StaticResource MyWaterMarkStyle}" Height="25" Tag="Water mark"/>
source to share