WPF NavigationService blanks PasswordBox.Password, which breaks the MVVM PasswordHelper
I am using three things that are just not friends:
- Pages and NavigationService
- Model-View-ViewModel design
- The PasswordBox control
Problem 1 – PasswordBox.Password is not a DependencyProperty
First off, Model-View-ViewModel is design centered around data binding. But PasswordBox.Password is not a DependencyProperty and therefore does not support binding. That is ok, a
PasswordBoxAssistant (alternately I have seen it named
PasswordBoxHelper) as described originally here and also here fixes seems to fix this.
That is, it fixes it unless you are using the NavigationService.
Problem 2 – NavigationService blanks PasswordBox.Password
See when the NavigationService navigates to another page, it somehow know that the current page has a PasswordBox and if it finds a PasswordBox, it blanks the password out. So since we are using PasswordBoxHelper to make MVVM and data binding work, the value is blanked in the ViewModel and Model as well.
For now, I happen to be using a custom button for navigation so I can simply do this in my ViewModel:
String tempPw = MyPassword; NavigationService.Navigate(NewPage); MyPassword = tempPw;
However, this is not the best solution. What if there were multiple links and different ways to navigate?
I think the best solution would be to figure out how to make
PasswordBoxAssistant handle this. But I am not sure how or if there is anyway to tell that the password was blanked by the NavigationService and to ignore binding in this instance.