在ResourceDictionary中使用ControlTemplate自定义Passwordbox后,密码属性为空



我已经通过ResourceDictionary自定义了我的项目TextBox。

<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>

现在我正在尝试将相同的设计添加到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>

我遇到的问题是,即使我输入了密码,我的Passwordbox的Password属性也是空的。如何访问密码?

WPF实际上在内部并不绑定到PasswordBoxPassword属性,而是始终绑定到名称为"的DependencyProperty<指定的名称>属性";。如果您查看PasswordBox类,则会发现一些DependencyProperty。

例如:public static readonly DependencyProperty CaretBrushProperty;

但你不会找到public static readonly DependencyProperty PasswordProperty;其中WPF可以与.结合

长话短说:密码是不可绑定的。

相关内容

  • 没有找到相关文章

最新更新