本文介绍下 PasswordBox 进行数据绑定的办法,本文参考链接。
本文残缺示例程序见 GitHub。
问题形容
PasswordBox 的 Password 属性不是依赖属性,因而无奈进行数据绑定。
解决办法
该问题的解决办法有多种,本文介绍如何通过增加附加属性解决该问题。
附加属性是说一个属性本不属于某个对象,但因为某种需要附加到该对象上,通过附加属性能够实现将属性与宿主解耦的目标。附加属性实质上就是依赖属性,只是它们在属性包装器和注册时有区别。注册附加属性应用 RegisterAttached 办法,注册依赖属性应用 Register 办法,这两个办法的参数差异并不大。
首先增加一个 PasswordBoxBindingHelper 类,该类蕴含一个附加属性(snippet:propa+ 两次 tab),通过设置该属性的 PropertyChangedCallback 将扭转告诉到 PasswordBox.Password,并通过增加对 PasswordBox.PasswordChanged 事件的响应来响应 PasswordBox.Password 的扭转。有了该附加属性,即可进行数据绑定。
public static string GetPasswordContent(DependencyObject obj) => (string)obj.GetValue(PasswordContentProperty);
public static void SetPasswordContent(DependencyObject obj, string value) => obj.SetValue(PasswordContentProperty, value);
public static readonly DependencyProperty PasswordContentProperty =
DependencyProperty.RegisterAttached("PasswordContent", typeof(string), typeof(PasswordBoxBindingHelper),
new PropertyMetadata(string.Empty, OnPasswordContentPropertyChanged));
private static void OnPasswordContentPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var box = d as PasswordBox;
box.PasswordChanged -= OnPasswordChanged;
var password = (string)e.NewValue;
if (box != null && box.Password != password)
box.Password = password;
box.PasswordChanged += OnPasswordChanged;
}
private static void OnPasswordChanged(object sender, RoutedEventArgs e)
{
var box = sender as PasswordBox;
SetPasswordContent(box, box.Password);
}
而后在 View 中应用该附加属性进行数据绑定,本文示例中主窗口蕴含一个 PasswordBox 控件及一个 Button 按钮:
// xaml 绑定附加属性
<Window ...
xmlns:local="clr-namespace:PasswordBoxBinding"
Title="PasswordBoxBinding" Height="300" Width="450" WindowStartupLocation="CenterScreen">
<Grid>
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
<PasswordBox MinWidth="200" Height="30" BorderBrush="LightGray" BorderThickness="2"
local:PasswordBoxBindingHelper.PasswordContent="{Binding Password,Mode=TwoWay}"/>
<Rectangle Width="20"/>
<Button Width="80" Height="30" Content="查看明码" Command="{Binding ClickedCommand}"/>
</StackPanel>
</Grid>
</Window>
//xaml.cs 设置绑定源
public MainWindow()
{InitializeComponent();
this.DataContext = new MainWindowViewModel();}
最初创立 ViewModel 进行逻辑解决:
// ViewModel
public class MainWindowViewModel : INotifyPropertyChanged
{
public string Password
{
get => _password;
set
{
_password = value;
OnPropertyChanged();}
}
public DelegateCommand ClickedCommand => _clickedCommand ?? (_clickedCommand = new DelegateCommand { ExecuteAction = OnClicked});
// 应用 CallerMemberName 个性简化代码,并能够防止手动输出谬误
public void OnPropertyChanged([CallerMemberName] string name = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
private void OnClicked(object o) => MessageBox.Show($"password: {Password}");
public event PropertyChangedEventHandler PropertyChanged;
private DelegateCommand _clickedCommand;
private string _password;
}
// 实现 ICommand
public class DelegateCommand : ICommand
{public bool CanExecute(object parameter) => CanExecuteAction?.Invoke(parameter) ?? true;
public void Execute(object parameter) => ExecuteAction?.Invoke(parameter);
public event EventHandler CanExecuteChanged;
public Action<object> ExecuteAction {get; set;}
public Func<object, bool> CanExecuteAction {get; set;}
}