因此,我写了一个UserControl
带有可绑定密码的 - SecureString
。这个代码UserControl
看起来像:
代码隐藏:
public partial class BindablePasswordBox : UserControl
{
public static readonly DependencyProperty SecurePasswordProperty = DependencyProperty.Register(
"SecurePassword", typeof(SecureString), typeof(BindablePasswordBox), new PropertyMetadata(default(SecureString)));
public SecureString SecurePassword
{
get { return (SecureString)GetValue(SecurePasswordProperty); }
set { SetValue(SecurePasswordProperty, value); }
}
public BindablePasswordBox()
{
InitializeComponent();
}
private void PasswordBox_OnPasswordChanged(object sender, RoutedEventArgs e)
{
SecurePassword = ((PasswordBox)sender).SecurePassword;
}
private void BindablePasswordBox_OnGotFocus(object sender, RoutedEventArgs e)
{
passwordBox.Focus();
}
}
XAML:
<UserControl x:Class="Sol.Controls.BindablePasswordBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
GotFocus="BindablePasswordBox_OnGotFocus">
<PasswordBox x:Name="passwordBox" PasswordChanged="PasswordBox_OnPasswordChanged"/>
</UserControl>