PasswordBoxバインディング
-
23-08-2019 - |
質問
私は、M-V-VMおよびWPFで開始し、いくつかの結合の問題を理解する問題を抱えて取得しています。
私はComboBox
とPasswordBox
を持っているログインページがあります。 ComboBox
は次のようになります:
<ComboBox Name="comboBox1" SelectedItem="{Binding Path=Username}">
これは正常に動作します - 私の値は毎回SelectedItem
上ComboBox
の変更を更新します。
私のViewModelで私は、ログインボタンがアクティブであるかどうかを判断するために、このメソッドを使用していますICommand
を持っています
public bool CanLogin()
{
return !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password);
}
私の問題は、私はViewModelににパスワードのプロパティにバインドPasswordBox
を持っていないですだから - 私はそれが更新されたときに指示する方法がありません。
それでは、どのように私は私のViewModelにPasswordBox
の値を得るのですか?私が読んだすべてがちょうどセキュリティ上の理由からPasswordBox
を結合しないと言います。私は単にCanLogin(上のパスワード制限を脱ぐだろう)が、私は値がAccountServiceのに沿って渡す必要があります。
解決
興味深います。
このブログの記事を見て、それはあなたを助けているかどうかを確認します。 http://blog.functionalfun.net/2008/06 /wpf-passwordbox-and-data-binding.htmlする
どうやらリンクはので、ここで今死んでいる元の溶液は、(ここではのた)です。
あなたはこのようなヘルパーを作成するために、添付プロパティを使用することができます:
public static class PasswordHelper { public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordHelper), new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged)); public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Attach", typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, Attach)); private static readonly DependencyProperty IsUpdatingProperty = DependencyProperty.RegisterAttached("IsUpdating", typeof(bool), typeof(PasswordHelper)); public static void SetAttach(DependencyObject dp, bool value) { dp.SetValue(AttachProperty, value); } public static bool GetAttach(DependencyObject dp) { return (bool)dp.GetValue(AttachProperty); } public static string GetPassword(DependencyObject dp) { return (string)dp.GetValue(PasswordProperty); } public static void SetPassword(DependencyObject dp, string value) { dp.SetValue(PasswordProperty, value); } private static bool GetIsUpdating(DependencyObject dp) { return (bool)dp.GetValue(IsUpdatingProperty); } private static void SetIsUpdating(DependencyObject dp, bool value) { dp.SetValue(IsUpdatingProperty, value); } private static void OnPasswordPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { PasswordBox passwordBox = sender as PasswordBox; passwordBox.PasswordChanged -= PasswordChanged; if (!(bool)GetIsUpdating(passwordBox)) { passwordBox.Password = (string)e.NewValue; } passwordBox.PasswordChanged += PasswordChanged; } private static void Attach(DependencyObject sender, DependencyPropertyChangedEventArgs e) { PasswordBox passwordBox = sender as PasswordBox; if (passwordBox == null) return; if ((bool)e.OldValue) { passwordBox.PasswordChanged -= PasswordChanged; } if ((bool)e.NewValue) { passwordBox.PasswordChanged += PasswordChanged; } } private static void PasswordChanged(object sender, RoutedEventArgs e) { PasswordBox passwordBox = sender as PasswordBox; SetIsUpdating(passwordBox, true); SetPassword(passwordBox, passwordBox.Password); SetIsUpdating(passwordBox, false); } }
それを使用します:
<PasswordBox w:PasswordHelper.Attach="True"
w:PasswordHelper.Password="{Binding Text, ElementName=plain, Mode=TwoWay}"
Width="100"/>
他のヒント
それは、バインド可能なパスワードボックスでを
私はここGISTのを掲示ます。
所属していません StackOverflow