Input Prompt support in WPF
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…
[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.
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….