-1
我想設置一個佔位符在TextBox
,我不知道爲什麼微軟不提供這個控件的默認屬性,很煩人的應用一種解決方法。無論如何,假設我有這種控制:不能在文本框上設置佔位符
<TextBox x:Name="Search" />
我已經創建了一個單獨的類來處理所有控制事件。這是它裏面的方法:
class ControlsHandle
{
MainWindow main = new MainWindow();
public void RemoveText(object sender, EventArgs e)
{
main.Search.Text = "";
}
public void AddText(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(main.Search.Text))
main.Search.Text = "Search a user...";
}
}
,這是MainWindow()
:
public MainWindow()
{
InitializeComponent();
Search.GotFocus += GotFocus.EventHandle(ControlsHandle.RemoveText);
Search.LostFocus += LostFocus.EventHandle(ControlsHandle.AddText);
}
不幸的是,我得到這個錯誤:
the UIElement.GotFocus event cannot be specified only on the left side of += or -=
在這條線:+= GotFocus.
只是簡單地添加佔位符= 「姓」 或什麼的。瀏覽器支持 佔位符。你不必做任何黑客就可以使它工作。 只有舊的瀏覽器不支持它。 如果你需要支持舊的瀏覽器,我會讓JavaScript支持這一點。 – Kiksen
@Kiksen這是wpf(xaml)不是html。 Xaml沒有佔位符屬性 – Dillinger