✅ Placeholder text in WPF control
Hi there, I want placeholder text like we have on html input fields in a WPF Textbox. I couldn't find a suitable property maybe I overlooked it. Any guidance?
How I do it right now is I handle this manually meaning I handle Focus and LostFocus events and set initial text brighter than user input and clear on Focus and set it back to the placeholder text on LostFocus in case there wasn't any input. I was hoping I wouldn't have to do this myself lol
6 Replies
hey there!
oof sorry, i misread your initial post. nvm wish you success
The keyword you are looking for is Watermark. It is implemented in MahApps. Take some inspiration from there, or use theirs controls.
the official microsoft recommendation is to set a background image
How to: Add a Watermark to a TextBox - WPF .NET Framework
Learn how to add a watermark to a TextBox via the included code examples in XAML, C#, and Visual Basic.
which is a bit heavy-handed
in theory, you could maybe do that same thing, but instead of with an image, you could build a
Brush
that draws from a Drawing
which is built from text
also seems a bit heavy-handed
if it were me, I'd probably swap out the default ControlTemplate
for TextBox
to see if I could inject a TextBlock
that sits behind the main TextBlock
that displays the .Text
property
which may involve having to subclass TextBox
to add a .Watermark
property that you can bind to
which may well be what MahApps doesnice ty @Denis and @ReactiveVeina
!closed
/closed