本文介绍下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进行逻辑解决:
// ViewModelpublic 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;}// 实现ICommandpublic 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; }}