Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place holder or watermark in TextBox windows 8

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

like image 701
Inder Kumar Rathore Avatar asked Aug 29 '12 06:08

Inder Kumar Rathore


People also ask

How do I add a Watermark to a TextBox?

On the Design tab, select Watermark > Custom Watermark. Choose Picture Watermark and select a picture, or choose Text watermark and type your watermark text in the Text box. Click OK.

How do you put a placeholder in a TextBox in Windows app?

First we need to expose the Windows SendMessage function. private const int EM_SETCUEBANNER = 0x1501; [DllImport("user32. dll", CharSet = CharSet. Auto)] private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.

What is Watermark in TextBox?

Set the TextBoxWatermarkExtender properties TargetControlID=" ", WatermarkText=" ". The "TargetControlID" property specifies the TextBox id, on which you want to set the watermark text, and "WatermarkText" accepts the text as watermark for that TextBox. Code for TextBoxWatermarkExtender.

How do I add a Watermark to a TextBox in WPF?

Go to the xaml code behind and add the Namespace to it, so that we can use the class WatermarkHelper. And add the following two Grids inside the LayoutRoot Grid. And then add TextBlocks and TextBoxes to the Grid. TextBlock is for the Watermark text and TextBox is for user input.


1 Answers

Edit for windows-8.1 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

like image 124
Inder Kumar Rathore Avatar answered Nov 09 '22 19:11

Inder Kumar Rathore