Place holder or watermark in TextBox windows 8

Inder Kumar Rathore picture Inder Kumar Rathore · Aug 29, 2012 · Viewed 11.8k times · Source

I want to show a placeholder text in TextBox when user hasn't typed anything and TextBox is idle.

In Andriod it can be done using android:hint="some Text"
In iPhone it can be done as textFild.placeholder = "some text";

How can I do it in windows 8 metro apps?

Thanks

Answer

Inder Kumar Rathore picture Inder Kumar Rathore · Aug 29, 2012

Edit for they have introduced a new property

<TextBox x:Name="UserName" PlaceholderText="User Name"/>

Please see Sergey Aldoukhov's answer


For me this is the working solution that I got.
If any one has better solution please answer.

private void OnTestTextBoxGotFocus(object sender, RoutedEventArgs e)
{
    if (testTextBox.Text.Equals("Type here...", StringComparison.OrdinalIgnoreCase))
    {
        testTextBox.Text = string.Empty;
    }  
}

private void OnTestTextBoxLostFocus(object sender, RoutedEventArgs e)
{
    if (string.IsNullOrEmpty(testTextBox.Text))
    {
        testTextBox.Text = "Type here...";
    }
}


MS also do the same check the example here.

P.S. I have created a custom control for TextBox you can download it from here