[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]
A specialized TextBox that displays a customizable "watermark" whenever its contents are empty and it does not have the focus.
Namespace:
System.Windows.Controls Assembly:
System.Windows.Controls.Extended (in System.Windows.Controls.Extended.dll)
Visual Basic (Declaration)
<TemplatePartAttribute(Name := "Focused State", Type := GetType(Storyboard))> _
<TemplatePartAttribute(Name := "Normal Watermarked State", Type := GetType(Storyboard))> _
<TemplatePartAttribute(Name := "Disabled State", Type := GetType(Storyboard))> _
<TemplatePartAttribute(Name := "Disabled Watermarked State", Type := GetType(Storyboard))> _
<TemplatePartAttribute(Name := "RootElement", Type := GetType(FrameworkElement))> _
<TemplatePartAttribute(Name := "MouseOver Watermarked State", Type := GetType(Storyboard))> _
<TemplatePartAttribute(Name := "WatermarkElement", Type := GetType(ContentControl))> _
<TemplatePartAttribute(Name := "Normal State", Type := GetType(Storyboard))> _
<TemplatePartAttribute(Name := "MouseOver State", Type := GetType(Storyboard))> _
Public Class WatermarkedTextBox _
Inherits TextBox
Dim instance As WatermarkedTextBox
[TemplatePartAttribute(Name = "Focused State", Type = typeof(Storyboard))]
[TemplatePartAttribute(Name = "Normal Watermarked State", Type = typeof(Storyboard))]
[TemplatePartAttribute(Name = "Disabled State", Type = typeof(Storyboard))]
[TemplatePartAttribute(Name = "Disabled Watermarked State", Type = typeof(Storyboard))]
[TemplatePartAttribute(Name = "RootElement", Type = typeof(FrameworkElement))]
[TemplatePartAttribute(Name = "MouseOver Watermarked State", Type = typeof(Storyboard))]
[TemplatePartAttribute(Name = "WatermarkElement", Type = typeof(ContentControl))]
[TemplatePartAttribute(Name = "Normal State", Type = typeof(Storyboard))]
[TemplatePartAttribute(Name = "MouseOver State", Type = typeof(Storyboard))]
public class WatermarkedTextBox : TextBox
[TemplatePartAttribute(Name = L"Focused State", Type = typeof(Storyboard))]
[TemplatePartAttribute(Name = L"Normal Watermarked State", Type = typeof(Storyboard))]
[TemplatePartAttribute(Name = L"Disabled State", Type = typeof(Storyboard))]
[TemplatePartAttribute(Name = L"Disabled Watermarked State", Type = typeof(Storyboard))]
[TemplatePartAttribute(Name = L"RootElement", Type = typeof(FrameworkElement))]
[TemplatePartAttribute(Name = L"MouseOver Watermarked State", Type = typeof(Storyboard))]
[TemplatePartAttribute(Name = L"WatermarkElement", Type = typeof(ContentControl))]
[TemplatePartAttribute(Name = L"Normal State", Type = typeof(Storyboard))]
[TemplatePartAttribute(Name = L"MouseOver State", Type = typeof(Storyboard))]
public ref class WatermarkedTextBox : public TextBox
public class WatermarkedTextBox extends TextBox
The watermark is typically text that indicates the purpose of the TextBox. For example, a text box might display the text "Search" until the user enters a search term.
A watermark does not have to be a String. Any object that derives from UIElement can be used. For example, an empty WatermarkedTextBox might display an image or a TextBlock with customized properties.
The following XAML creates a page with two WatermarkedTextBox controls. The first WatermarkedTextBox control uses a simple text watermark and the second uses a control as a watermark.
<UserControl x:Class="WatermarkedTextBoxExample.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400"
Height="300"
>
<Canvas x:Name="LayoutRoot" Background="White">
<!-- Explanatory labels. -->
<TextBlock Text="WatermarkedTextBox Demonstration" Margin="0,20,10,20"
FontFamily="Verdana" FontSize="18" FontWeight="Bold"
Foreground="#FF5C9AC9" Canvas.Left="20" Canvas.Top="0"/>
<TextBlock Canvas.Top="60" Canvas.Left="20" Text="WatermarkedTextBox with a simple text watermark:" />
<TextBlock Canvas.Top="140" Canvas.Left="20" Text="WatermarkedTextBox with a Button as a watermark:" />
<!-- A TextBox with a simple text watermark. -->
<WatermarkedTextBox Watermark="This is a watermark."
Canvas.Top="90" Canvas.Left="20"
Height="30" Width="200"/>
<!-- A TextBox with a custom watermark. -->
<!-- Any UIElement can serve as a watermark. -->
<WatermarkedTextBox Canvas.Top="170" Canvas.Left="20"
Height="75" Width="300">
<WatermarkedTextBox.Watermark>
<Button Height="60" Width="260" Content="This button serves as a watermark, too!" />
</WatermarkedTextBox.Watermark>
</WatermarkedTextBox>
</Canvas>
</UserControl>
Run this sample.
System..::.Object
System.Windows.Controls..::.Control
System.Windows.Controls..::.TextBox
System.Windows..::.DependencyObject
System.Windows..::.FrameworkElement
System.Windows..::.UIElement
System.Windows.Controls..::.WatermarkedTextBox
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Reference
Other Resources