// 在获得焦点时,验证文本内容是否与提示信息相同,相同则清除,并改变文本颜色
private void email_GotFocus(object sender, RoutedEventArgs e)
{
if (this.email.Text.Equals("email@example.com"))
{
this.email.Text = "";
}
this.email.Foreground = new SolidColorBrush(Colors.Black);
}
// 在失去焦点时,验证文本内容是否为空(清除空格),为空则重新设置提示信息,并改变颜色
private void email_LostFocus(object sender, RoutedEventArgs e)
{
if (0 == this.email.Text.Trim().Length)
{
this.email.Text = "email@example.com";
this.email.Foreground = new SolidColorBrush(Colors.Gray);
}
}
// 效果图