Hussein Khalil Hussein Khalil - 1 year ago 150
C# Question

Adding default / initial text to a Textbox

Is it possible to add a "default" text value to a

in WPF, with the text disappearing when the user clicks on the control or types anything in it ?

If it's not possible, how would I create a template which allows a user to define a text box with distinct properties for the content of the control and its initial value?

Answer Source

This is certainly possible. You can either:

1) Set the initial text normally, then on an event such as OnClick() or OnTextSelected() you can delete that text, then it will be blank before they start typing. You can also go one step further and add something to OnTextChanged() to see whether the text is blank and every time there is no content, your initial content can be set again.

2) Alternatively, you can create a custom style for your text block, with a text block inside of it, a property to set the text block text and an event hooked up to clear the text block text. This is nicer, but more work. I would recommend this if you wish to re-use this control.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download