locked
textbox with input prompt? RRS feed

  • Question

  •  How to make textbox display a input prompt?
    I'm try to make a textbox with a prompt in it like "Type here". As soon as you click in the box, the prompt disappears, leaving an empty textbox where you can type your search string.
    i'm using the Blend 2.5.
    Wednesday, September 10, 2008 7:13 AM

All replies

  • Hi Sonnial,

    Here's how I would go about it:

    1. Edit the template for the TextBox (or edit the style, so you can reuse it later)
    2. Add a TextBlock positioned just over the area where the user will type (there should be a ScrollViewer called "PART_ContentHost")
    3. Set the IsHitTestVisible property to false on the TextBlock so that it doesn't get in the way of clicking on the Text
    4. Setup a trigger so that when the Text property is empty (""), the TextBlock's Visibility is Visible, and it is Collapsed by default
    Let me know if you need futher help on these steps.

    Paul
    Wednesday, September 10, 2008 11:30 PM
  • Hi Paul!
    I tried your method, questions here:
    How can I get the text property of the TextBox when it is empty? Do i need to write some code? 

    Rightnow, I had two triggers in my program. 
    One trigger to the TextBox that when it is "Lostfocus", the TextBlock's Visibility is Visible.
    And one trigger to the TextBox that when it is "Gotfocus", the TextBlock's Visibility is Collapsed.
    The prompt works well when TextBox got focused and lost foused. But after I type some words in the textbox and click somewhere else, the prompt comes back made the text overlapping....  
    Thursday, September 11, 2008 8:31 AM
  • Hi Sonnial,

    You should  be able to do it with a Trigger on the ControlTemplate. Something like this:

    <Trigger Property="Text" Value="">
       <Setter Property="Visibility" TargetName="_hintOverlayTextBlock" Value="Collapsed" />
    </Trigger>

    Paul
    Thursday, September 11, 2008 9:08 AM