如何在第一次点击文本框时删除文本?

时间:2014-10-23 12:16:37

标签: c# wpf xaml

我试图制作简单的Windows移动应用。其中有登录和注册

我想知道如何在用户关注或点击它时将此textbox.text值代码删除。就像我们在HTML5中做的那样。

<input type="text" name="user" placeholder="User Name "/>

我们如何在html5中做文本消失。我想知道如何在窗口应用程序中执行此操作

<TextBox Height="72" HorizontalAlignment="Left" Margin="28,254,0,0" 
         Name="Username" Text="" VerticalAlignment="Top" Width="422" />

3 个答案:

答案 0 :(得分:3)

您可以使用GotFocus事件:

<TextBox Height="72" HorizontalAlignment="Left" Margin="28,254,0,0" 
         Name="UsernameTextBox" Text="" VerticalAlignment="Top" Width="422" 
         GotFocus="YourGotFocusEvent"/>

代码背后:

public void YourGotFocusEvent(object sender, RoutedEventArgs e)
{
   UsernameTextBox.Text = string.empty;
   // if you want this to happen only the first time you can remove the event handler like this
   UsernameTextBox.GotFocus -= YourGotFocusEvent;
}

答案 1 :(得分:0)

或处理Click事件。双击设计器中的按钮,然后编写

this.Text= string.empty

答案 2 :(得分:0)

对于水印样式文本框,您可以查看以下UI库。 MahApps为文本框提供了一组附加属性,允许您轻松地进行水印。

查看此链接 - &gt; http://mahapps.com/controls/textbox.html