首页 > 解决方案 > WPF Forms - 如何在文本框上添加文本占位符

问题描述

一般来说,我对 WPF 和 C# 很陌生。我正在尝试在我的文本框上添加一个文本框,当您专注于文本框时,我希望文本消失,如果我移动到其他内容(也就是失去焦点),并且文本为空,我希望文本框添加文本我想要文本返回。

但我在 GotFocus.EventHandle(RemoveText); 上收到错误 和 LostFocus.EventHandle(AddText); 关于“GotFocus”和“Lostfocus”这两个词(在下面的代码中)

我错过了什么?

我尝试了下一个代码:

ExcelPath.Text = "Please Drag Excel into here";
ExcelPath.GotFocus += GotFocus.EventHandle(RemoveText);
ExcelPath.LostFocus += LostFocus.EventHandle(AddText);

public void RemoveText(object sender, EventArgs e)
{
    if (ExcelPath.Text == "Please Drag Excel into here")
    {
        ExcelPath.Text = "";
    }
}

public void AddText(object sender, EventArgs e)
{
    if (string.IsNullOrWhiteSpace(ExcelPath.Text))
        ExcelPath.Text = "Please Drag Excel into here";
}

标签: c#wpfforms

解决方案


这是连接事件处理程序的语法:

ExcelPath.Text = "Please Drag Excel into here";
ExcelPath.GotFocus += RemoveText;
ExcelPath.LostFocus += AddText;

摆脱GotFocus.EventHandle那是什么。


推荐阅读