Monday, November 16, 2009

How to implement TextBox with PlaceHolder in WPF

Download source files - here

Using PlaceHolder is very useful thing for UI in any type application. I want to show a short way how can you implement it in WPF.
You can custom TextBox and add place-holder text on MouseEnter and remove on MouseLeave events. In this case place-holder's text looks like original text.. You can experiment with Foreground property, but truth me - you will not be satisfy.
Let my suggest other way. You can use AdornerLayer for host the place-holder text. In this case you should create class that is inherit from Adorner and this class should show your place-holder text on AdornerLayer. I've create little sample that demonstrate this.

Unfortunate, AdornerLayer doesn't presented over any Popup controls. So this solution isn't worked if you put our customized TextBox control in any Popup and as item in Menu or ComboBox controls.

No comments: