PasswordBox Bindung
-
23-08-2019 - |
Frage
Ich bin nur mit M-V-VM und WPF und mit Fragen zu verstehen, einige verbindlichen Probleme getting started.
Ich habe eine Login-Seite, die ein ComboBox
und PasswordBox
hat. Die ComboBox
sieht wie folgt aus:
<ComboBox Name="comboBox1" SelectedItem="{Binding Path=Username}">
Das funktioniert ganz gut - meine Werte jedes Mal die SelectedItem
Änderungen auf der ComboBox
erhalten aktualisiert
In meinem Viewmodel Ich habe ein ICommand
, die dieses Verfahren verwendet, um zu bestimmen, ob die Login-Button aktiv ist:
public bool CanLogin()
{
return !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password);
}
Also mein Problem ist, ich habe nicht den PasswordBox
auf die Passwort-Eigenschaft auf dem Viewmodel gebunden habe - so habe ich keine Möglichkeit zu sagen, wenn es aktualisiert wird,
Wie bekomme ich den Wert des PasswordBox
zu meinem Viewmodel? Alles, was ich gelesen habe, sagt gerade keine PasswordBox
aus Sicherheitsgründen binden. Ich würde einfach das Passwort Beschränkung des CanLogin starten (), aber ich brauche, um den Wert zusammen mit einem Account passieren.
Lösung
Interessant.
Blick auf dieser Blog-Post und sehen, ob es Ihnen hilft. http://blog.functionalfun.net/2008/06 /wpf-passwordbox-and-data-binding.html
Anscheinend ist der Link tot ist jetzt so hier ist die ursprüngliche Lösung (gefunden hier ):
Sie können die angefügten Eigenschaften verwenden, um einen Helfer wie diese zu erstellen:
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); } }
Verwenden Sie es:
<PasswordBox w:PasswordHelper.Attach="True"
w:PasswordHelper.Password="{Binding Text, ElementName=plain, Mode=TwoWay}"
Width="100"/>