当第一次点击文本框时,如何删除文本框中的文本

本文关键字:文本 删除 何删除 第一次 | 更新日期: 2023-09-27 18:27:30

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

我想知道如何对这个文本框进行编码。当用户聚焦或点击它时,文本值会被删除。就像我们在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" />

当第一次点击文本框时,如何删除文本框中的文本

您可以使用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;
}

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

this.Text= string.empty

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

请参阅此链接->http://mahapps.com/controls/textbox.html