首页 > 解决方案 > 在 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 的密码属性也是空的。如何访问密码?

标签: c#wpfcontroltemplateresourcedictionarypasswordbox

解决方案


WPF 实际上并不在内部绑定到Password属性PasswordBox,而是始终绑定到DependencyProperty名称为“< nameYouSpecified >Property”的属性。如果你看一下这个PasswordBox类,就会有一些 DependencyProperties。

例如: public static readonly DependencyProperty CaretBrushProperty;

但是您不会找到 public static readonly DependencyProperty PasswordProperty; WPF 可以绑定到的位置。

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


推荐阅读