WPF PasswordBox password數據綁定
阿新 • • 發佈:2017-06-14
out xmlns win == mpat pwd pan present formats
WPF的PasswordBox控件的Password屬性不是依賴屬性,無法直接進行數據綁定,為使其在MVVM模式中正常使用,可以為PasswordBox增加一個助手類,步驟如下:
原文鏈接:http://blog.csdn.net/ryb666666/article/details/7629767
1.新建一個PasswordBoxHelper 幫助類
1 public static class PasswordBoxHelper 2 { 3 public static readonly DependencyProperty PasswordProperty = 4View CodeDependencyProperty.RegisterAttached("Password", 5 typeof(string), typeof(PasswordBoxHelper), 6 new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged)); 7 public static readonly DependencyProperty AttachProperty = 8 DependencyProperty.RegisterAttached("Attach", 9 typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false, Attach)); 10 private static readonly DependencyProperty IsUpdatingProperty = 11 DependencyProperty.RegisterAttached("IsUpdating", typeof(bool), 12 typeof(PasswordBoxHelper)); 13 14public static void SetAttach(DependencyObject dp, bool value) 15 { 16 dp.SetValue(AttachProperty, value); 17 } 18 public static bool GetAttach(DependencyObject dp) 19 { 20 return (bool)dp.GetValue(AttachProperty); 21 } 22 public static string GetPassword(DependencyObject dp) 23 { 24 return (string)dp.GetValue(PasswordProperty); 25 } 26 public static void SetPassword(DependencyObject dp, string value) 27 { 28 dp.SetValue(PasswordProperty, value); 29 } 30 private static bool GetIsUpdating(DependencyObject dp) 31 { 32 return (bool)dp.GetValue(IsUpdatingProperty); 33 } 34 private static void SetIsUpdating(DependencyObject dp, bool value) 35 { 36 dp.SetValue(IsUpdatingProperty, value); 37 } 38 private static void OnPasswordPropertyChanged(DependencyObject sender, 39 DependencyPropertyChangedEventArgs e) 40 { 41 PasswordBox passwordBox = sender as PasswordBox; 42 passwordBox.PasswordChanged -= PasswordChanged; 43 if (!(bool)GetIsUpdating(passwordBox)) 44 { 45 passwordBox.Password = (string)e.NewValue; 46 } 47 passwordBox.PasswordChanged += PasswordChanged; 48 } 49 private static void Attach(DependencyObject sender, 50 DependencyPropertyChangedEventArgs e) 51 { 52 PasswordBox passwordBox = sender as PasswordBox; 53 if (passwordBox == null) 54 return; 55 if ((bool)e.OldValue) 56 { 57 passwordBox.PasswordChanged -= PasswordChanged; 58 } 59 if ((bool)e.NewValue) 60 { 61 passwordBox.PasswordChanged += PasswordChanged; 62 } 63 } 64 private static void PasswordChanged(object sender, RoutedEventArgs e) 65 { 66 PasswordBox passwordBox = sender as PasswordBox; 67 SetIsUpdating(passwordBox, true); 68 SetPassword(passwordBox, passwordBox.Password); 69 SetIsUpdating(passwordBox, false); 70 } 71 }
2.在View層XAML綁定
1 <PasswordBox x:Name="passwordBox" PasswordChar="*" Helper:PasswordBoxHelper.Attach="True" 2 Helper:PasswordBoxHelper.Password="{Binding Path=Pwd,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
1 <Window x:Class="HR_Test01.View.LoginView" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:Helper="clr-namespace:HR_Test01.Model" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 7 xmlns:local="clr-namespace:HR_Test01.View" 8 mc:Ignorable="d" AllowsTransparency="True" WindowStyle="None" MouseLeftButtonDown="Window_MouseLeftButtonDown" 9 Title="登錄界面" Height="250" Width="400" WindowStartupLocation="CenterScreen">
WPF PasswordBox password數據綁定