Input Prompt support in WPF
Intro
If you look at Vista User experience guide, one thing that is a must have is the Input Prompt. I think that this is very important to improve the user experience. Yet unfortunately this is not supported natively by WPF
But don’t worry because AvalonControlsLibrary is at the rescue
I had noting to do today so I decided to create this functionality and share it with you guys… You would say, aha; here comes Marlon with a new control. But NO. This is not a control as such. What I have created is a class with some attached properties that can be used to have the Prompt Input feature for any control (or better any control that would make sense such a feature). I choose the attached properties approach rather than creating a custom control for the simple reason to support any control that you wish to have an input prompt like the one displayed in the image below.
As you can see in the image above I am using the Input Prompt both for the TextBox and the ComboBox. And yes you can use it with any control that you want…
How to use this…
Well using this feature is quite easy, all you have to do is to set an attached property in your control. Something like this…
<TextBox local:InputPrompt.PromptText="Enter Search" />
[where local is the XAML namespace mapping]
In total there are 8 properties that you can set which are
- InputPrompt.PromptText – I guess you already figured this one out
- InputPrompt.PromptColor – This is a brush used as foreground for the Prompt Text (Default: Gray)
- InputPrompt.PromptFontFamily – The font family to use for the Prompt Text (Default: Arial)
- InputPrompt.PromptFontSize – The font size of the Prompt Text (Default: 12.0)
- InputPrompt.PromptFontStretch – The font stretch to use for the Prompt Text (Default: FontStretches.Normal)
- InputPrompt.PromptFontStyle – The font style to use for the Prompt Text (Default: FontStyles.Normal)
- InputPrompt.PromptFontWeight – The font weight to use for the Prompt Text (Default: FontWeights.Normal)
- InputPrompt.PromptBackColor – This is a brush that is used to color the background of the prompt (Default: Transparent)
Please note: You must set the InputPrompt.PromptText in order for this to work all the other properties are optional. Also you must have you control under an AdornerDecorator because the InputPrompt will draw the prompt text in an adorner.
If you use the Input Prompt with a textbox the input prompt will be much more intelligent. What do I mean by this? Basically the Input Prompt handles the Text Changed event of the TextBox and if the user enters no text the prompt re appears. The same happens if the TextBox looses focus and there is no text in it. I did this behaviour for the TextBox because this is the standard behaviour for an Input Prompt for textboxes.
How does this work behind the scenes
Well it’s quite simple. Basically an Adorner is placed on top of the control which sets the Attached properties and draws the InputPrompt.PromptText. Besides drawing the text this also draws a rectangle covering the surface of the control so that as soon as the user click the control(or better mouse down on the control), the adorner disappears and the control gets activated once again.
When the InputPrompt.PromptText is set for a TextBox, the input prompt will register to the LostFocus event and the TextChanged event and make sure to display the Prompt Text if the Text of the TextBox is to empty.
Conclusion
All this will be included in AvalonControlsLibrary v3 but I decided to upload a demo as a stand alone so that you can start using this immediately.
Hope you like this and as usual I am all ears for feedback and suggestions….
Thanks






Hi Marlon,
Nice work… have you seen the WatermarkTextBox in Silverlight 2.0? Very similar!
Regards,
Rudi
Hi there,
Thanks for the kind comment. If I might add; this control is not only for TextBoxes you can use it for any WPF control. yet again for TextBoxes this control acts more intelligently
[...] Grech discusses how to add Input Prompt support in Vista with WPF using Avalon Controls [...]
Pingback by Visual Studio Links #8 : Mostly Programming Stuff | March 11, 2008 |
YES YES YES!!! AWESOME!!! Can’t believe this is not built-in functionality and search high and nigh for a solution. Great to see you put the effort into making this happen. You saved my day!
Ahh… It does appear that the ComboBox suffers from a bug, unfortunately. When the mousedown occurs, the box just gets focused… it doesn’t drop down (so it requires two clicks to get the list of items). I’m SEVERELY new to WPF so I’m not sure how to fire a click event on the box… that should do the trick…
Anyone got a fix for combo box ?
mmm… I guess that this is because of the Combobox Control Template… Will look into it…
Marlon,
How would I call this via c# as a popup form from a main form and specify the “Enter Search” message?
Thanks in advance,
Nigel
same way you would do any other attached property from code. Have a look at http://msdn.microsoft.com/en-us/library/ms749011.aspx#attached_properties_code
Nice utility!
A minor side-effect is that the Tooltip is not shown when the InputPrompt is active.
Also is typing on char and deleting this the input prompt is shown again and input focus of the underlying control is lost as can be seen in your test program.
We use a custom template with a border for TextBox.
The Input Prompt is shown a bit higher than the normal text.
An Offset property would be nice to adjust this.
Part of control template:
Hi Marlon,
Have you tried using wpf datagrid with Entity Framework? If so, are you successful in editing the data in the grid as a combobox? I can do that if I don’t use the EF as a datasource. If you are then can you make a download source code and demo for it. Thank you!
Your site is very very cool !! I love it
Respect !,
This is good news, I’m really looking forward to playing around with it once the beta opens up.,