Focus-dependent text change for TextBoxes in WPF

Posted by Simon on Stack Overflow See other posts from Stack Overflow or by Simon
Published on 2010-06-12T11:30:34Z Indexed on 2010/06/12 11:52 UTC
Read the original article Hit count: 239

Filed under:
|
|
|
|

Hey there.

I'm writing an application in WPF using the MVVM-pattern and will really often use TextBoxes. I don't want to use labels for the user to know user what the text box is for, i.e. I don't want something like this:

<TextBlock> Name: </TextBlock>
<TextBox />

Instead, I would like the TextBox to contain its own label. Statically, you would express it like this:

<TextBox>Name</TextBox>

If the cursor is displayed in the textbox, i.e. the TextBox gains focus, I want the description text to disappear. If the TextBox is left empty and it loses the focus, the description text should be shown again. It's similar to the search textbox of StackOverflow or the one of Firefox. (please tell me if your not sure what I mean).

One TextBox's label may change at runtime, dependending on e.g. a ComboBox's selected element or a value in my ViewModel. (It's like in Firefox's search TextBox, if you select google from the search engins' menu, the TextBox's label changes to "Google", if you select "Yahoo" its set to "Yahoo"). Thus I want to be able to bind the label's content.

Consider that I may already have a Binding on the Text-Property of the TextBox.

How can implement such a behaviour and make it reusable for any of my TextBox's? Code is welcome but not needed; a description of what to do is enough.

Thank you in advance.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET