0

I'm trying to implement placeholder text for PasswordBox by following code:

<PasswordBox x:Name="passwordText"/>
<TextBlock IsHitTestVisible="False" Text="Password">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Password, ElementName=passwordText}" Value="">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

Here's the result:

enter image description here

So, is there something wrong with the code above?

BTW, I tried it with TextBox instead of PasswordBox and it worked just as expected.

Zolbayar
  • 896
  • 12
  • 29

3 Answers3

0

EDIT #1

After user clemens comment, I corrected the language of my answer.

There is no change notification with Password property, so it will not work here. Password property is not a DP.

You have to use plain method of event handlers. Use PasswordBox.PasswordChangedevent.

Community
  • 1
  • 1
AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
  • There is no binding **to** the Password property. Password is the source property of the Binding, and hence does not need to be a dependency property. However, since it is no DP, there is also no change notification that would trigger the Binding of the DataTrigger. – Clemens Oct 05 '16 at 08:31
  • @Clemens I didnt notice my language, corrected it now. – AnjumSKhan Oct 05 '16 at 08:44
  • After tweaking around with the code @Gopichandar posted, now I'm determined to use `PasswordChanged` event. Thanks for your suggestion – Zolbayar Oct 05 '16 at 10:04
0

As Mentioned by @Anjum. Password property in PasswordBox is not a Dependency Property for some security reasons. So it won't notify for any changes in Trigger.

Kind of workaround will be

<Window.Resources>
    <Style TargetType="{x:Type PasswordBox}" xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <Style.Resources>
            <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                <VisualBrush.Visual>
                    <Label Content="Password" Foreground="LightGray"/>
                </VisualBrush.Visual>
            </VisualBrush>
        </Style.Resources>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=Password}" Value="{x:Null}">
                <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
            </DataTrigger>

            <Trigger Property="IsKeyboardFocused" Value="True">
                <Setter Property="Background" Value="White" />
            </Trigger>
        </Style.Triggers>
        <Setter Property="Control.Foreground" Value="#4C2C66"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>
</Window.Resources>

<PasswordBox Width="200" Height="75" />

Source:

https://stackoverflow.com/a/20342141/2819451

https://stackoverflow.com/a/27167280/2819451

Community
  • 1
  • 1
Gopichandar
  • 2,742
  • 2
  • 24
  • 54
0

As @AnjumSKhan suggested, I've done it with PasswordBox.PasswordChanged event. In case of someone has similar problems, here's the code for adding placeholder for PasswordBox.

In xaml file:

<Window.Resources>
    <VisualBrush x:Key="PasswordPlaceHolderBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
        <VisualBrush.Visual>
            <Label Content="Нууц үг" Foreground="DarkGray"/>
        </VisualBrush.Visual>
    </VisualBrush>
</Window.Resources>

<PasswordBox PasswordChanged="passwordText_PasswordChanged" 
    Background="{StaticResource PasswordPlaceHolderBrush}"  x:Name="passwordText"/>

In code behind:

private void passwordText_PasswordChanged(object sender, RoutedEventArgs e)
{
    PasswordBox senderOb = (PasswordBox)sender; 
    if(senderOb.Password == "")
    {
        passwordText.Background = (VisualBrush) FindResource("PasswordPlaceHolderBrush");
    }
    else
    {
        passwordText.Background = Brushes.White; 
    }

}
Zolbayar
  • 896
  • 12
  • 29