How to add "type here" message to WPF textbox?

If the text field does not have keyboard focus and does not contain text, then display italicized gray text "enter here" "inside". How? What does not work:

  • Setting the property Text

    - this will mess up the data binding.
  • Template overriding - it's messy and overridden anyway.

It is desirable that the solution be as much XAML and as little C # as possible. Perhaps I could create an attached property that would do the magic?

+2


source to share


5 answers


What you are looking for is known as a Watermark . I'm not familiar with WPF, but searching for WPF and Watermark on "google" returns tons of results.



+4


source


The best solution I've seen so far is InfoTextBox by Kevin WPF Bag-o-Tricks Library . Check it.



+1


source


Not the cleanest solution, but you can always add an TextBlock

overlay TextBox

and change it Visible

when it TextBox

gains or loses focus.

A nasty hack, but somewhat innovative? Can I get cake points ?; -)

0


source


Style your TextBox. This is one of the easiest ways to add a watermark to your TextBox. The code creates a simple style and associates it with the TextBox.

(Code extracted from: http://social.msdn.microsoft.com/Forums/vstudio/en-US/d565c79e-6b4a-44e2-a566-a4a622eab22f/how-to-set-watermark-for-textbox-in-simple -way? forum = wpf )

<Window.Resources>
    <Style x:Key="MyWaterMarkStyle" TargetType="{x:Type TextBox}">           
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Grid>
                        <Border Background="White" BorderBrush="#FF7D8683" BorderThickness="1"/>
                        <ScrollViewer x:Name="PART_ContentHost" Margin="5,0,0,0" VerticalAlignment="Center" />
                        <Label Margin="5,0,0,0" x:Name="WaterMarkLabel" Content="{TemplateBinding Tag}" VerticalAlignment="Center"
                           Visibility="Collapsed" Foreground="Gray" FontFamily="Arial"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="Text" Value=""/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Visibility" TargetName="WaterMarkLabel" Value="Visible"/>
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Foreground" Value="DimGray"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

...

<TextBox Style="{StaticResource MyWaterMarkStyle}" Height="25" Tag="Water mark"/>

      

0


source


I recommend using Syncfusion for WPF. It contains SfTextBoxExt which supports watermarks and other features.

Look at here

0


source







All Articles