c# - 在 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 的密码属性也是空的。如何访问密码?
解决方案
WPF 实际上并不在内部绑定到Password
属性PasswordBox
,而是始终绑定到DependencyProperty
名称为“< nameYouSpecified >Property”的属性。如果你看一下这个PasswordBox
类,就会有一些 DependencyProperties。
例如:
public static readonly DependencyProperty CaretBrushProperty;
但是您不会找到
public static readonly DependencyProperty PasswordProperty;
WPF 可以绑定到的位置。
长话短说:密码不可绑定。
推荐阅读
- kubernetes - 当 `cgroup_manager=systemd` 时使用 cri-o 运行 pod 和容器
- python - 如何从数据集中的其余部分中选择一行并将其添加到python中的初始矩阵中?
- javascript - 如果操作数有小数,正则表达式可防止逗号分隔符
- java - 如何使自动递增数字Java
- angular - 集成 Google 地图 API 时,Angular5 应用程序未在 IE11 中加载
- react-final-form - 当外部组件的状态发生变化时,改变 React 最终表单字段的值
- java - GlobalFilter 与 WebFilter
- python - 从 itertools 组合列表结果中删除括号
- mysql - 为什么 MYSQL AND oparator 在尝试提取同一列中的数据时不起作用?
- python - 是否有可以从 Excel 中获取的数据中去除撇号的功能?