![]() Then update the PasswordBox declaration to use the PasswordBoxHelper as follows: Xmlns:local="clr-namespace:CsWpfMvvmPasswordBox" First, add the following attribute to the Window declaration in the XAML so that we can access our project’s classes (adjust namespace as needed): You will also need to add the following usings at the top: SetPassword(passwordBox, passwordBox.Password) Private static void PasswordChanged(object sender, RoutedEventArgs e) Private static void Attach(DependencyObject sender, ![]() PasswordBox.PasswordChanged += PasswordChanged PasswordBox.Password = (string)e.NewValue PasswordBox.PasswordChanged -= PasswordChanged PasswordBox passwordBox = sender as PasswordBox Private static void OnPasswordPropert圜hanged(DependencyObject sender, Private static void SetIsUpdating(DependencyObject dp, bool value) Return (bool)dp.GetValue(IsUpdatingProperty) Private static bool GetIsUpdating(DependencyObject dp) Public static void SetPassword(DependencyObject dp, string value) Return (string)dp.GetValue(PasswordProperty) Public static string GetPassword(DependencyObject dp) Return (bool)dp.GetValue(AttachProperty) Public static bool GetAttach(DependencyObject dp) Public static void SetAttach(DependencyObject dp, bool value) Private static readonly DependencyProperty IsUpdatingProperty =ĭependencyProperty.RegisterAttached("IsUpdating", typeof(bool), Typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, Attach)) Public static readonly DependencyProperty AttachProperty =ĭependencyProperty.RegisterAttached("Attach", New FrameworkPropertyMetadata(string.Empty, OnPasswordPropert圜hanged)) Public static readonly DependencyProperty PasswordProperty =ĭependencyProperty.RegisterAttached("Password", To get up and running, let’s add a new PasswordBoxHelper class to our project, and add the implementation from the second link: Well, no problem! It turns out that there actually is an MVVM-friendly way to bind the password – see the PasswordBoxAssistant and PasswordHelper implementations. You see, the Password property can’t be bound specifically because it shouldn’t, but let’s say that we’re like most other developers and we’re so blinded by this MVVM dogma that we don’t care about the security concerns and we want an MVVM-friendly solution. Oh no! What are we going to do now? If we can’t bind the password, and have to somehow retrieve it from the control, then we’ll break the MVVM pattern, right? And we MVVM knights-in-shining-armour can’t afford to deviate from the path of light that is MVVM. Now, you’ll notice right away that something’s wrong when you see the blue squiggly line at the password binding: Let’s go with this (just make sure the namespace matches what you have): Now we can build our form by editing the XAML in MainWindow.xaml. Right, now let’s add a couple of properties to our MainWindowViewModel that we’ll want to bind to: This.DataContext = new MainWindowViewModel() ), set up your window’s DataContext by adding the following line at the end of your constructor: Create a new WPF application, and then add a new class called MainWindowViewModel. Right, so let’s set up something we can work with. This article is a little advanced in nature, and assumes you’re familiar with WPF and MVVM. And I’m also going to show you why it’s a very bad idea to do this. In this article, I’m going to show how the password can be bound in the ViewModel despite this limitation. The thing is that you can access the password entered by the user using the Password property, but it’s not a dependency property, and MVVM purists don’t like the fact that they can’t bind it to their ViewModel. In WPF circles, the PasswordBox control has generated quite a bit of discussion. The original premise of this article was that binding the password would expose it, but it turns out that it is already exposed regardless of whether you use data binding or not. Update 1st October 2018: As ZXeno posted in the comments, there seems to be a security flaw in the PasswordBox control by which it is possible to snoop passwords directly. This article was originally posted here at Programmer’s Ranch with the title “C# WPF/MVVM: Why You Shouldn’t Bind PasswordBox Password”, on 4th October 2014.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |