开发者

Adding default / initial text to a Textbox

Is it possible to add a "default" text value to a TextBox 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 v开发者_JAVA技巧alue?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜