Empty Password Property after customizing Passwordbox with ControlTemplate in ResourceDictionary

203 Views Asked by At

I've customized my projects TextBox via a ResourceDictionary.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Style x:Key="TextBoxTheme" TargetType="{x:Type TextBox}">
    <Style.Setters>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TextBox">
                    <Border CornerRadius="10" 
                            BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="2" 
                            Background="#FF62B6CB">
                        <Grid>
                            <TextBox VerticalContentAlignment="Center" 
                                     HorizontalContentAlignment="Left" 
                                     Padding="5,0,5,0" 
                                     Background="Transparent" 
                                     BorderThickness="0"
                                     Foreground="#1B4965"
                                     Margin="1"
                                     TextWrapping="Wrap"
                                     FontWeight="Bold"
                                     Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, UpdateSourceTrigger=PropertyChanged}"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="TextWrapping" Value="Wrap" />
    </Style.Setters>
</Style>

now I'm trying to add the same design to the Passwordbox

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Style x:Key="PasswordBoxTheme" TargetType="{x:Type PasswordBox}">
    <Style.Setters>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="PasswordBox">
                    <Border CornerRadius="10" 
                            BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="2" 
                            Background="#FF62B6CB">
                        <Grid>
                            <PasswordBox VerticalContentAlignment="Center" 
                                         HorizontalContentAlignment="Left" 
                                         Padding="5,0,5,0" 
                                         Background="Transparent" 
                                         BorderThickness="0"
                                         Foreground="#1B4965"
                                         Margin="1"
                                         FontWeight="Bold" />
                                         <!--Password="{Binding RelativeSource= {RelativeSource TemplatedParent}, Path=Password, UpdateSourceTrigger=PropertyChanged}"/-->
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style.Setters>
</Style>

The Problem I'm having is that the Password Property of my Passwordbox is empty even though I entered a password. How can I access the Password?

1

There are 1 best solutions below

0
On

WPF does not bind to the Password proptery of the PasswordBox in fact internally, but always to a DependencyProperty with the name "< nameYouSpecified >Property". If you look at the PasswordBox class there are some DependencyProperties.

For examle: public static readonly DependencyProperty CaretBrushProperty;

But you will not find a public static readonly DependencyProperty PasswordProperty; where WPF could bind to.

Long story short: The Password is not bindable.