在文本框窗口8中放置持有者或水印

Ind*_*ore 16 c# microsoft-metro windows-8 .net-4.5

我希望在TextBox用户没有键入任何内容并且TextBox处于空闲状态时显示占位符文本.

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

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

谢谢

Ind*_*ore 24

编辑他们引入了一个新属性

<TextBox x:Name="UserName" PlaceholderText="User Name"/>
Run Code Online (Sandbox Code Playgroud)

请参阅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...";
    }
}
Run Code Online (Sandbox Code Playgroud)


MS也在这里检查示例.

PS我已经创建了一个自定义控件,TextBox你可以从这里下载它


Ser*_*hov 8

在Windows 8.1中,TextBox具有PlaceholderText属性:

<TextBox
    x:Name="UserName"
    PlaceholderText="User Name"
    />
Run Code Online (Sandbox Code Playgroud)


Ost*_*tan 6

WinRT Xaml工具包有一个WatermarkTextbox控件:http: //winrtxamltoolkit.codeplex.com/

您也可以通过NuGet获取它,它还有其他几个有用的控件.

你可以通过在你的Page's属性中引用工具包来使用它:

xmlns:xtk="using:WinRTXamlToolkit.Controls"
Run Code Online (Sandbox Code Playgroud)

只需访问WaterMarkTextBox,如下所示:

<xtk:WatermarkTextBox WatermarkText="some text" />
Run Code Online (Sandbox Code Playgroud)

编辑.:

Callisto也提供了水印TextBox:https: //github.com/timheuer/callisto

它只是在自述文件中没有提到.