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
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With