✅ 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
Candyroll93
Candyroll932y ago
hey there! oof sorry, i misread your initial post. nvm wish you success
Denis
Denis2y ago
The keyword you are looking for is Watermark. It is implemented in MahApps. Take some inspiration from there, or use theirs controls.
JakenVeina
JakenVeina2y ago
the official microsoft recommendation is to set a background image
JakenVeina
JakenVeina2y ago
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 does
Florian Voß
Florian VoßOP2y ago
nice ty @Denis and @ReactiveVeina !closed /closed
Want results from more Discord servers?
Add your server