How to make the text disappear from the WPF C#field

Asked

Viewed 661 times

1

I’m using wpf for the first time, as is the declaration of a textbox field in which I have a text (Ex: "Login") and when I click it disappear and soh the pointer so I start typing?

If you don’t understand what that text is example from Hotmail

3 answers

1

Works perfectly:

<Grid>
<TextBox  Width="250"  VerticalAlignment="Center" HorizontalAlignment="Left" x:Name="SearchTermTextBox" Margin="5"/>
<TextBlock IsHitTestVisible="False" Text="Insira o texto aqui" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0,0,0" Foreground="DarkGray">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Text, ElementName=SearchTermTextBox}" Value="">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

Follow an example below:

inserir a descrição da imagem aqui

When inserting a text the watermark disappears...

inserir a descrição da imagem aqui

  • Raccoon, you have to do all this back for that O.O?

  • Yes... just copy and paste it works. In Textblock you change the Text property with the text you want.

  • I did not understand why they were negative, since the example does exactly the same behavior as the Hotmail.com

1


Since you’re starting out, I recommend you try to implement the functionality as the other answers suggest, just to better understand the mechanisms of the platform.

When safe, check the library Extendend WPF Toolkit, which has several very useful WPF components. One of them is called Watermarktextbox and provides exactly the functionality you need. If you need this behavior in more than one place of your application, I recommend using the library instead of implementing it manually.

Examples of how to use the component can be found in the second link.

There is, however, a difference as to your original request: in this component, the original text (in your example, "Login") only disappears when the user starts typing. A similar behavior can be observed in several Stackoverflow text boxes, such as the 'Edit Summary' and 'search' box'.

0

The recommended is to do as the friend quoted in the other answer ...but you can try via code.... try this: create a method for watermark and call this method in your constructor.

   private void marcaDagua(){
        txtLogin.Text = "Login";
        txtLogin.Foreground = Brushes.LightGray;
    }

call him at the counter

 public SuaClasse() {
        InitializeComponent();
        marcaDagua();
    }

later in your Textbox’s Getfocus event write the code

   private void txtLogin_GotFocus(object sender, RoutedEventArgs e) {
        txtLogin.Text = string.Empty;
        txtLogin.Foreground = Brushes.Black;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.