0

I'm wanting to set a custom theme to my password box so that it looks a bit more modern. I want the text inside the password box to say "password" and when you start typing in the box it should disappear, I managed this with a normal TextBox just fine, however, when using a similar method for the passwordbox I get different results. Instead of the "password" placeholder text disappearing, it just stays there permanently even though I am typing in the box.

Here is my style:

<Style TargetType="{x:Type PasswordBox}"
       x:Key="ModenPasswordBox">

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type PasswordBox}">
                <Border CornerRadius="10"
                        Background="#353340">
                    <Grid>
                        <Rectangle StrokeThickness="1"/>
                        <PasswordBox Margin="1"
                                     BorderThickness="0"
                                     Background="Transparent"
                                     VerticalContentAlignment="Center"
                                     HorizontalContentAlignment="Center"
                                     Padding="5"
                                     Foreground="#cfcfcf"
                                     x:Name="PasswordBox"/>
                        <TextBlock IsHitTestVisible="False"
                                   Text="{TemplateBinding Name}"
                                   VerticalAlignment="Center"
                                   HorizontalAlignment="Center"
                                   FontSize="11"
                                   Foreground="DarkGray">

                            <TextBlock.Style>
                                <Style TargetType="{x:Type TextBlock}"
                                       x:Name="PasswordText">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Path=Password, ElementName=PasswordBox}" Value="{x:Null}">
                                            <Setter Property="Visibility" Value="Visible"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                    <Setter Property="Visibility" Value="Hidden"/>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                    </Grid>

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

Setting the data binding to PasswordChar does not work and I tried changing the Value="" to Value="{x:Null}" with no luck. I even tried to change from a data trigger to just a regular trigger, however, I cannot set the property binding to the property of a different element (i.e. setting a trigger inside the TextBlock that says Property="{Binding Path=PasswordChanged, ElementName=PasswordBox}" is not allowed.

I'm still looking around, if I can find the solution I will post it here, thank you!

  • Look at that answer: https://stackoverflow.com/questions/1607066/wpf-watermark-passwordbox-from-watermark-textbox – Auditive Oct 07 '21 at 11:13

0 Answers0