PasswordBox value is not updating in the viewModel. I tried following the solution stated in How to bind to a PasswordBox in MVVM But facing some issues
Off Below code stated , I'm able to trigger the password change event and see the password value in string AddTempPassword of viewModel before Button click OK. But once the button is clicked the AddUserRequest method within View Model contains null value for AddTempPassword.
For sure I am missing something, Will someone please help guide.
XAML Code
<Label Content="Username"/>
<TextBox Text="{Binding AddUsername}"/>
<Label Content="Temporary Password" />
<PasswordBox PasswordChanged="PasswordBox_PasswordChanged"/>
<Button Content="OK" Command="{Binding AddUserCommand}"/>
Code Behind
public partial class SecurityWindow : Window
{
public SecurityWindow()
{
InitializeComponent();
SecurityViewModel vm = new SecurityViewModel();
DataContext = vm;
}
private void PasswordBox_PasswordChanged(object sender,
RoutedEventArgs e)
{
if (this.DataContext != null)
{
((dynamic)this.DataContext).AddTempPassword =
((PasswordBox)sender).Password;
}
}
View Model
public class SecurityViewModel : ObservableItem, INotifyPropertyChanged
{
public SecurityViewModel()
{
AddUserCommand = new RelayCommand(a => AddUserRequest());
}
public string AddUsername { get; set; }
public ICommand AddUserCommand { get; set; }
private string _addTempPassword;
public string AddTempPassword
{
get { return _addTempPassword; }
set
{
_addTempPassword = value;
OnPropertyChanged(nameof(AddTempPassword));
}
}
private void AddUserRequest()
{
try
{
bool flag = Framework.SecurityManager.AddUser(AddUsername,
**AddTempPassword**, profileName);
if (!flag)
{
MessageBox.Show(@"User was not able to be added. Note:
Usernames must be unique.");
}
else
{
MessageBox.Show(@"The user " + AddUsername + @" was
added.");
}
}
catch (Exception ex)
{
Logbook.WriteLog(Logbook.EventLevel.Error, ex);
}
}
}
RelayCommand fetched from another class
public class RelayCommand : ICommand
{
#region Fields
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of <see cref="ICommand"/>.
/// </summary>
/// <param name="execute">Delegate to execute when Execute is called on the command. This can be null to just hook up a CanExecute delegate.</param>
/// <remarks><seealso cref="CanExecute"/> will always return true.</remarks>
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
/// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
#endregion
#region ICommand Members
///<summary>
///Defines the method that determines whether the command can execute in its current state.
///</summary>
///<param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
///<returns>
///true if this command can be executed; otherwise, false.
///</returns>
public bool CanExecute(object parameter) => _canExecute == null || _canExecute(parameter);
/// <summary>
/// Occurs when changes occur that affect whether or not the command should execute.
/// </summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
///<summary>
///Defines the method to be called when the command is invoked.
///</summary>
///<param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to <see langword="null" />.</param>
public void Execute(object parameter) => _execute(parameter);
#endregion
}
When OK Button is clicked I am expecting the AddTempPassword Value read to be in String when AddUserRequest method is called. I Have rest of the code taking care of encryption. I suspect, seeing some other object updated before button OK click i.e. the one via Datacontext behind the code.
AddTempPassword Actual value before AddUserRequest Method call