I want to create a TextBox, which would have a gray “default” text appear in it, if it’s
a) empty
b) has lost focus
when the user enters the text box, the gray “default” text should dissappear.
I’ve tried to do this using ControlTemplate.Triggers, but I can’t seem to find HasFocus property.
What is the best way to do this using XAML?
Whilst there is no real benefit in re-inventing the wheel, it might be interesting to see how this can be done. The easiest way to do this (in pure XAML) is to create a
ControlTemplatefor theTextBoxthat overlays aTextBlockwhen it is not focussed and does not contain text:The
MultiTriggermeans “set Visibility to Visible if theTextproperty is empty AND the TextBox does not have keyboard focus”If you want to make this more reusable then you could create a custom control with this as it’s default template and with a Dependency Property containing the prompt message