TextBox windows 8中的占位符或水印 [英] Place holder or watermark in TextBox windows 8

查看:23
本文介绍了TextBox windows 8中的占位符或水印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户没有输入任何内容并且 TextBox 空闲时,我想在 TextBox 中显示占位符文本.

在 Andriod 中可以使用 android:hint="some Text"
在 iPhone 中,它可以作为 textFild.placeholder = "some text";

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";

如何在 Windows 8 Metro 应用程序中执行此操作?

How can I do it in windows 8 metro apps?

谢谢

推荐答案

Edit for 他们引入了一个新属性

Edit for windows-8.1 they have introduced a new property

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

请参阅 Sergey Aldoukhov 的回答

对我来说,这是我得到的有效解决方案.
如果有人有更好的解决方案请回答.

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 也做同样的检查 这里的例子.

P.S.我为 TextBox 创建了一个自定义控件,您可以从 here 下载它/strong>

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

这篇关于TextBox windows 8中的占位符或水印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆