Поделиться через


DatePickerTextBox.Watermark Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the content displayed as a watermark in the DatePickerTextBox when it is empty.

Namespace:  System.Windows.Controls.Primitives
Assembly:  System.Windows.Controls (in System.Windows.Controls.dll)

Syntax

'Declaration
Public Property Watermark As Object
public Object Watermark { get; set; }
<controlsPrimitives:DatePickerTextBox>
  <controlsPrimitives:DatePickerTextBox.Watermark>
    singleUIElement
  </controlsPrimitives:DatePickerTextBox.Watermark>
</controlsPrimitives:DatePickerTextBox>
<controlsPrimitives:DatePickerTextBox Watermark="uistring"/>

XAML Values

  • singleUIElement
    A single UIElement (such as Path or Image) that displays the intended watermark content.

  • uistring
    A string that provides the watermark content.

Property Value

Type: System.Object
The content displayed as a watermark in the DatePickerTextBox when it is empty.

Remarks

The Watermark property detects whether the DatePicker format is set to Long or Short and provides the correct long or short format hint to the user.

Examples

The following examples show how to change the watermark value displayed in a DatePicker control by deriving from DatePicker, retrieving the textbox part and setting its Watermark property. This change only affects the empty DatePicker, before a date is selected. Once a date is selected the date is then removed, the DatePicker reverts to the default behavior of either a long or short format hint.

Run this sample

' Create a class that inherits from DatePicker.
Public Class MyDatePicker
    Inherits DatePicker

    ' Override OnApplyTemplate to retrieve the DatePickerTextBox part of the template.
    Public Overrides Sub OnApplyTemplate()
        MyBase.OnApplyTemplate()
        Dim box As DatePickerTextBox = TryCast(MyBase.GetTemplateChild("TextBox"), DatePickerTextBox)

        Dim formatString As String = ""
        If Me.SelectedDateFormat = DatePickerFormat.Short Then
            formatString = "mm/d/yy"
        End If
        If Me.SelectedDateFormat = DatePickerFormat.Long Then
            formatString = "dddd, MMMM, dd, yyyy"
        End If

        ' Set the Watermark property.
        box.Watermark = "Type ( " & formatString & ") or select a date --> "
    End Sub
End Class
// Create a class that inherits from DatePicker.
public class MyDatePicker : DatePicker
{
  // Override OnApplyTemplate to retrieve the DatePickerTextBox part of the template.
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        DatePickerTextBox box = base.GetTemplateChild("TextBox") as DatePickerTextBox;

        string formatString = "";
        if (this.SelectedDateFormat == DatePickerFormat.Short)
            formatString = "mm/d/yy";
        if (this.SelectedDateFormat == DatePickerFormat.Long)
            formatString = "dddd, MMMM, dd, yyyy";

        // Set the Watermark property.
        box.Watermark = "Type ( " + formatString + " or select a date --> ";
    }
}

The following XAML shows how to use the derived DatePicker in a page.

    <UserControl x:Class="DatePickerWatermarkVB.MainPage"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="https://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local ="clr-namespace:DatePickerWatermarkVB"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" 
    xmlns:sdk="https://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
    <Grid x:Name="LayoutRoot" Background="White">
        <local:MyDatePicker Width="240" Height="30" />
    </Grid>
</UserControl>

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.