Ankit Yadav

Ankit Yadav

  • NA
  • 762
  • 38.9k

How to add Placeholder or Watermark to a Password box in WPF

Feb 5 2019 5:13 AM
I want to add placeholder to a password box i have achived that uptp some extent but the problem is that when i type the password, then the watermark text remains there like the image attached below:
  1. <Border  Margin="0,0,0,10"  Grid.Column="0" Grid.Row="4" CornerRadius="7" BorderThickness="1" Grid.ColumnSpan="2" BorderBrush="Gray">  
  2.              <PasswordBox  Grid.Row="1" Margin="3,1,1,1" Grid.Column="1" x:Name="txtConfirmPassword"  BorderThickness="0,0,0,0"  VerticalContentAlignment="Center" HorizontalContentAlignment="Left"  Height="35" >  
  3.                  <PasswordBox.Style>  
  4.                      <Style xmlns:sys="clr-namespace:System;assembly=mscorlib" TargetType="{x:Type PasswordBox}">  
  5.                          <Style.Resources>  
  6.                              <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" Stretch="None">  
  7.                                  <VisualBrush.Visual>  
  8.                                      <Label Content="Confirm Password" Foreground="Gray" Padding="10"/>  
  9.                                  </VisualBrush.Visual>  
  10.                              </VisualBrush>  
  11.                          </Style.Resources>  
  12.                          <Style.Triggers>  
  13.                              <DataTrigger Binding="{Binding Password}" Value="{x:Null}">  
  14.                                  <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />  
  15.                              </DataTrigger>  
  16.                              <Trigger Property="IsKeyboardFocused" Value="True">  
  17.                                  <Setter Property="Background" Value="White"/>  
  18.                              </Trigger>  
  19.                          </Style.Triggers>  
  20.                      </Style>  
  21.                  </PasswordBox.Style>  
  22.              </PasswordBox>  
  23.          </Border>  
 

Answers (4)