Reputation: 1903
I'm trying to set a placeholder on TextBox
, I don't know why Microsoft doesn't provide a default property for this control, is very annoying apply a workaround. Anyway, suppose that I've this control:
<TextBox x:Name="Search" />
I've created a separate class to handle all control event. This is the method inside it:
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...";
}
}
and this is the MainWindow()
:
public MainWindow()
{
InitializeComponent();
Search.GotFocus += GotFocus.EventHandle(ControlsHandle.RemoveText);
Search.LostFocus += LostFocus.EventHandle(ControlsHandle.AddText);
}
Unfortunately I got this error:
the UIElement.GotFocus event cannot be specified only on the left side of += or -=
on this line: += GotFocus.
Upvotes: 1
Views: 3346
Reputation: 519
Try this
<TextBox Name="search" Text="Search a user..." GotFocus="search_GotFocus"/>
private void search_GotFocus(object sender, RoutedEventArgs e)
{
search.Text = "";
}
Upvotes: 3