Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 861705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:01:18+00:00 2026-05-15T09:01:18+00:00

I’m writing an application in WPF using the MVVM-pattern and will really often use

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:01:19+00:00Added an answer on May 15, 2026 at 9:01 am

    Here is a style I think is exactly what you are looking for, and it’s pure XAML.

    <Style x:Key="WatermarkTextBox" TargetType="{x:Type TextBox}">
         <Setter Property="Template">
              <Setter.Value>
                   <ControlTemplate TargetType="{x:Type TextBox}">
                        <Grid>
                             <Border x:Name="BorderBase" Background="White" BorderThickness="1.4,1.4,1,1" BorderBrush="Silver"> 
                                 <Label x:Name="TextPrompt" 
                                    Content="{Binding RelativeSource={RelativeSource  Mode=TemplatedParent}, Path=Tag}" 
                                    Background="{TemplateBinding Background}" Visibility="Collapsed" 
                                    Focusable="False" Foreground="Silver"/>
                             </Border>
                             <ScrollViewer Margin="0" x:Name="PART_ContentHost" Foreground="Black"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                             <MultiTrigger>
                                  <MultiTrigger.Conditions>
                                       <Condition Property="IsFocused" Value="False"/>
                                       <Condition Property="Text" Value=""/>
                                  </MultiTrigger.Conditions>
                                  <Setter Property="Visibility" TargetName="TextPrompt" Value="Visible"/>
                             </MultiTrigger>
                             <Trigger Property="IsFocused" Value="True">
                                  <Setter Property="BorderBrush" TargetName="BorderBase" Value="Black"/>
                             </Trigger>
                             <Trigger Property="IsEnabled" Value="False">
                                  <Setter Property="Foreground" Value="DimGray" />
                             </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
               </Setter.Value>
          </Setter>
     </Style>
    

    Usage is:

    <TextBox Style="{StaticResource WatermarkTextBox}" Tag="Full Name"/>
    

    where Tag is the help message you want to show.

    You could clean up this style for your own use, but the most important part is the which controls hiding/showing the helper text.

    It’s worth noting as well, there is already a DependencyObject available for storing the helper text, so you don’t need to create your own with this method.

    FrameworkElement.Tag is available for holding arbitrary information about this element. That’s why we set the Tag property:

    http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.tag.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 430k
  • Answers 430k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use $uid = $this->Auth->user('id'); Check the api documentation:… May 15, 2026 at 2:01 pm
  • Editorial Team
    Editorial Team added an answer That's a Java annotation defining the strategy the annotated entity… May 15, 2026 at 2:01 pm
  • Editorial Team
    Editorial Team added an answer I discovered the issue that was causing this. The web… May 15, 2026 at 2:01 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.