How to make text disappear from WPF field C #

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 on it it adds up and is just the pointer for me to start typing ?

If you did not understand how this text is hotmail example

    
asked by anonymous 20.05.2015 / 15:15

3 answers

1

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

When you're sure, check out the Extendend WPF Toolkit library, which has several 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 manually deploying.

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. Similar behavior can be seen in several StackOverflow text boxes, such as the 'Editing Summary' and 'search' box.

    
20.05.2015 / 15:37
1

It 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>

Here is an example below:

Wheninsertingatextthewatermarksome...

    
20.05.2015 / 15:19
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 the watermark and call this method in your constructor.

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

call it on the flyer

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

Then in the GetFocus event of your TextBox write the code

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