C# 在ResourceDictionary中使用ControlTemplate自定义Passwordbox后,密码属性为空
我已经通过ResourceDictionary定制了我的项目文本框C# 在ResourceDictionary中使用ControlTemplate自定义Passwordbox后,密码属性为空,c#,wpf,controltemplate,resourcedictionary,passwordbox,C#,Wpf,Controltemplate,Resourcedictionary,Passwordbox,我已经通过ResourceDictionary定制了我的项目文本框 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <Style x:Key="TextBoxTh
<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>
现在我正在尝试向密码箱添加相同的设计
<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实际上在内部并不绑定到
密码框的密码
属性,而是始终绑定到名为“属性”的从属属性。如果您查看PasswordBox
类,就会发现一些依赖属性
例如:
公共静态只读从属属性CaretBrushProperty代码>
但是你找不到一个
公共静态只读DependencyProperty PasswordProperty代码>
WPF可以绑定到的位置
长话短说:密码不可绑定