I know about those MVVM purists and PasswordBox binding problem but I'm handling my login via codebehind and it works but for the life of me I can't get over using the DataContext from codebehind.
This event is attached to the PasswordChanged event of my passwordbox. Can this be handled in another way?
private void CopyPassword(object sender, RoutedEventArgs e)
{
if(e.Source is PasswordBox)
{
var source = e.Source as PasswordBox;
if(source.DataContext is LoginViewModel)
{
var vm = source.DataContext as LoginViewModel;
vm.Password = source.Password;
}
}
}
Yes it can be handled in many ways. There is nothing wrong with code-behind. Your current solution has the problem that you're tying concrete viewmodel with PasswordBox
which makes it NON reusable for other viewmodels.
Better way is to write attached property with event listeners.
There are many resources that can get you started, check the related links on right.