GotFocus和LostFocus替代品/替代品

时间:2017-05-12 05:36:10

标签: wpf

我正在四处寻找如何在WPF上做占位符,我发现this回答(第一个)。

答案中给出的代码在这里:

        TextBox myTxtbx = new TextBox();
        myTxtbx.Text = "Enter text here...";

        myTxtbx.GotFocus += GotFocus.EventHandle(RemoveText);
        myTxtbx.LostFocus += LostFocus.EventHandle(AddText);

        void RemoveText(object sender, EventArgs e)
        {
            myTxtbx.Text = "";
        }

        void AddText(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(myTxtbx.Text))
                myTxtbx.Text = "Enter text here...";
        }

当我输入我的代码时,我收到以下错误:

The event 'UIElement.GotFocus' can only appear on the left hand side of += or -=

The event 'UIElement.LostFocus' can only appear on the left hand side of += or -=

我知道错误意味着什么,但我不知道如何解决错误并仍能获得所需的结果。非常感谢帮助!

0 个答案:

没有答案