Place holder or watermark in TextBox windows 8

11,922

Solution 1

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

Solution 2

In Windows 8.1, TextBox has a PlaceholderText property:

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

Solution 3

The WinRT Xaml Toolkit has a WatermarkTextbox Control: http://winrtxamltoolkit.codeplex.com/

You can get it via NuGet as well, it comes with several other usefull Controls.

you can use it by referncing the toolkit in your Page´s property´s:

xmlns:xtk="using:WinRTXamlToolkit.Controls"

And the just access the WaterMarkTextBox like this:

<xtk:WatermarkTextBox WatermarkText="some text" />

Edit.:

Callisto offers a watermark TextBox too: https://github.com/timheuer/callisto

Its just not mentioned in the readme yet.

Solution 4

In case you are using WPF, you are looking for a watermark, check out the following stackoverflow answer

Solution 5

What you can do is you can set a default text and clear it using tapped event of textbox or you can use the watermark text box see here

Share:
11,922
Inder Kumar Rathore
Author by

Inder Kumar Rathore

Develops mobile applications (primarily iOS) Open source work https://github.com/InderKumarRathore LinkedIn profile https://in.linkedin.com/in/InderKumarRathore Personal Site: http://inderkumar.herokuapp.com 📧rathore619🌀gmail🔘com But before emailing please read Jon Skeet's blog post on Stack Overflow-related emails first. Blogs Property in Objective - C

Updated on June 15, 2022

Comments

  • Inder Kumar Rathore
    Inder Kumar Rathore about 2 years

    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

  • Nicolas Voron
    Nicolas Voron about 11 years
    Note that callisto provides a WatermarkTextbox, too. It's lighter than the WinRT Xaml Tollkit (which is a little bit "heavy" for just a such control).
  • Ostkontentitan
    Ostkontentitan about 11 years
    Thx i will add it to my answer!