0

I wrote a PasswordBox style.I want to do hint text in passwordbox but when the lost focus ,the hint text is appering.Where I'm doing mistake, How can I solve this problem?

Here is code:

 <Style TargetType="{x:Type PasswordBox}"  xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <Style.Resources>
        <VisualBrush  x:Key="Bg1" AlignmentX="Center" AlignmentY="Center" Stretch="None" >
            <VisualBrush.Visual>
                <Label Content="Enter password" Foreground="LightGray" Margin="5,0,0,0"/>
            </VisualBrush.Visual>
        </VisualBrush>
        <VisualBrush  x:Key="Bg2" AlignmentX="Center" AlignmentY="Center" Stretch="None" >
            <VisualBrush.Visual>
                <Label Content="" Foreground="LightGray" Margin="5,0,0,0"/>
            </VisualBrush.Visual>
        </VisualBrush>
    </Style.Resources>
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="Foreground" Value="#FF585858"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type PasswordBox}">
                <Border CornerRadius="2" x:Name="border" Background="{TemplateBinding Background}" BorderThickness="1" BorderBrush="LightGray">
                    <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Visible" Margin="0" ScrollViewer.CanContentScroll="True" x:Name="PART_ContentHost"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="BorderBrush" TargetName="border" Value="#FFD0D0D0"/>
                        <Setter Property="BorderThickness" TargetName="border" Value="2"/>
                    </Trigger>

                    <DataTrigger Binding="{Binding Path=Password}" Value="{x:Static sys:String.Empty}">
                        <Setter Property="Background" Value="{StaticResource Bg2}" />
                    </DataTrigger>

                    <DataTrigger Binding="{Binding Path=Password}" Value="{x:Null}">
                        <Setter Property="Background" Value="{StaticResource Bg1}" />
                    </DataTrigger>                      

                    <Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter Property="Background" Value="White" />
                    </Trigger>                      

                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>   

Thanks for your advice.

MLElyakan
  • 249
  • 4
  • 25

2 Answers2

2

Password property in PasswordBox is not a Dependency Property for some security reasons. So it cannot be used in Trigger. The triggers you applied for Password property were not working. That is the reason, the hint text is appearing even if you have valid text in it. Only the trigger applied on IsKeyboardFocused is working, regardless of whether Password is empty or NULL.

The following post must be giving some good idea to implement watermark for Password box,

http://prabu-guru.blogspot.in/2010/06/how-to-add-watermark-text-to-textbox.html

Jawahar
  • 4,775
  • 1
  • 24
  • 47
-2

Hope the following links may help you:

http://www.codeproject.com/Articles/26977/A-WatermarkTextBox-in-3-lines-of-XAML

Create WPF Watermark in Pure XAML

Community
  • 1
  • 1
Siva Gopal
  • 3,474
  • 1
  • 25
  • 22