WPF PasswordBox進(jìn)行數(shù)據(jù)綁定方法
本文介紹下PasswordBox進(jìn)行數(shù)據(jù)綁定的方法,本文參考鏈接。
本文完整示例程序見GitHub。
問(wèn)題描述
PasswordBox的Password屬性不是依賴屬性,因此無(wú)法進(jìn)行數(shù)據(jù)綁定。
解決辦法
該問(wèn)題的解決辦法有多種,本文介紹如何通過(guò)添加附加屬性解決該問(wèn)題。
附加屬性是說(shuō)一個(gè)屬性本不屬于某個(gè)對(duì)象,但由于某種需求附加到該對(duì)象上,通過(guò)附加屬性可以實(shí)現(xiàn)將屬性與宿主解耦的目的。附加屬性本質(zhì)上就是依賴屬性,只是它們?cè)趯傩园b器和注冊(cè)時(shí)有區(qū)別。注冊(cè)附加屬性使用RegisterAttached方法,注冊(cè)依賴屬性使用Register方法,這兩個(gè)方法的參數(shù)差別并不大。
首先添加一個(gè)PasswordBoxBindingHelper類,該類包含一個(gè)附加屬性(snippet:propa+兩次tab),通過(guò)設(shè)置該屬性的PropertyChangedCallback將改變通知到PasswordBox.Password,并通過(guò)添加對(duì)PasswordBox.PasswordChanged事件的響應(yīng)來(lái)響應(yīng)PasswordBox.Password的改變。有了該附加屬性,即可進(jìn)行數(shù)據(jù)綁定。
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中使用該附加屬性進(jìn)行數(shù)據(jù)綁定,本文示例中主窗口包含一個(gè)PasswordBox控件及一個(gè)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 設(shè)置綁定源 public MainWindow() { InitializeComponent(); this.DataContext = new MainWindowViewModel(); }
最后創(chuàng)建ViewModel進(jìn)行邏輯處理:
// ViewModel public class MainWindowViewModel : INotifyPropertyChanged { public string Password { get => _password; set { _password = value; OnPropertyChanged(); } } public DelegateCommand ClickedCommand => _clickedCommand ?? (_clickedCommand = new DelegateCommand { ExecuteAction = OnClicked }); // 使用CallerMemberName特性簡(jiǎn)化代碼,并可以避免手動(dòng)輸入錯(cuò)誤 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; } // 實(shí)現(xiàn)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; } }
以上就是WPF PasswordBox進(jìn)行數(shù)據(jù)綁定方法的詳細(xì)內(nèi)容,更多關(guān)于WPF PasswordBox數(shù)據(jù)綁定的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#實(shí)現(xiàn)的簡(jiǎn)單整數(shù)四則運(yùn)算計(jì)算器功能示例
這篇文章主要介紹了C#實(shí)現(xiàn)的簡(jiǎn)單整數(shù)四則運(yùn)算計(jì)算器功能,涉及C#界面布局、事件響應(yīng)及數(shù)值運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09C#創(chuàng)建磁性窗體的實(shí)現(xiàn)方法
經(jīng)常會(huì)遇到一種情況,即當(dāng)拖動(dòng)一個(gè)窗體(主窗體)時(shí),其他窗體(子窗體)隨著該窗體移動(dòng),當(dāng)拖動(dòng)子窗體時(shí),其他窗體將不跟隨移動(dòng),這就是磁性窗體,所以本文給大家介紹了C#創(chuàng)建磁性窗體的實(shí)現(xiàn)方法,需要的朋友可以參考下2024-04-04C#導(dǎo)出數(shù)據(jù)到excel如何提升性能
這篇文章主要介紹了C#導(dǎo)出數(shù)據(jù)到excel如何提升性能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07C#使用BitConverter與BitArray類進(jìn)行預(yù)定義基礎(chǔ)類型轉(zhuǎn)換
這篇文章介紹了C#使用BitConverter與BitArray類進(jìn)行預(yù)定義基礎(chǔ)類型轉(zhuǎn)換的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05C#讀取XML中元素和屬性值的實(shí)現(xiàn)代碼
用C#讀取xml有很多方式,這里我就先使用XmlDocument讀取Xml,用一段代碼遍歷所有元素,并打印student的所有屬性和子節(jié)點(diǎn)的值2013-04-04c#菜單動(dòng)態(tài)合并的實(shí)現(xiàn)方法
這篇文章主要介紹了c#菜單動(dòng)態(tài)合并的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10