Julio Motol Julio Motol - 1 year ago 87
C# Question

How do I create a textbox hint, simmiliar to SO's search box

I'm creating a login window for a wpf application and for its textbox, what I want to do is the same as how the 'Search Q&A' on the upper right of the Stack Overflow website behaves.

There is a text box containing a text 'Username' and when you focus, the text it will still be displayed, but when you start to type, the text disappear and your input will be displayed. then when you try to erase it back and nothing is in the textbox, it will revert back to the text 'Username'.

Jim Jim
Answer Source

The term you are looking for is a Textbox Hint

Try this:

    <VisualBrush x:Key="SearchHint" TileMode="None" Stretch="None" AlignmentX="Left">
            <TranslateTransform X="5" Y="0" />
                <TextBlock FontStyle="Italic" Foreground="Black" Opacity="0.3" Text="Search …"/>

    <TextBox VerticalContentAlignment="Center"  Height="30">
            <Style TargetType="TextBox">
                    <Trigger Property="Text" Value="">
                        <Setter Property="Background" Value="{StaticResource SearchHint}"/>


enter image description here

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