using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Threading; using UtopiaCanteenSystem.ViewModels; namespace UtopiaCanteenSystem.Views; /// /// Minimal code-behind for PasswordBox + initial focus. /// public partial class AdminSettingsAuthView : UserControl { private AdminSettingsAuthViewModel? _vm; public AdminSettingsAuthView() { InitializeComponent(); Loaded += OnLoaded; DataContextChanged += OnDataContextChanged; } private void OnLoaded(object sender, RoutedEventArgs e) { Dispatcher.BeginInvoke(() => { // If VM has a remembered password, prefill the PasswordBox (it's not bindable). if (DataContext is AdminSettingsAuthViewModel vm && !string.IsNullOrEmpty(vm.Password)) PasswordBox.Password = vm.Password; UsernameTextBox.Focus(); UsernameTextBox.SelectAll(); }, DispatcherPriority.Input); } private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { if (_vm != null) _vm.PropertyChanged -= VmOnPropertyChanged; _vm = DataContext as AdminSettingsAuthViewModel; if (_vm != null) _vm.PropertyChanged += VmOnPropertyChanged; // Sync PasswordBox from VM when navigating here (supports remembered creds). if (_vm != null) PasswordBox.Password = _vm.Password ?? string.Empty; } private void VmOnPropertyChanged(object? sender, PropertyChangedEventArgs e) { if (!string.Equals(e.PropertyName, nameof(AdminSettingsAuthViewModel.Password), StringComparison.Ordinal)) return; if (_vm != null && string.IsNullOrEmpty(_vm.Password) && !string.IsNullOrEmpty(PasswordBox.Password)) PasswordBox.Password = string.Empty; } private void PasswordBox_OnPasswordChanged(object sender, RoutedEventArgs e) { if (DataContext is AdminSettingsAuthViewModel vm) vm.Password = PasswordBox.Password; } private void Confirm_OnKeyDown(object sender, KeyEventArgs e) { if (e.Key != Key.Enter) return; if (DataContext is AdminSettingsAuthViewModel vm && vm.ConfirmCommand.CanExecute(null)) { vm.ConfirmCommand.Execute(null); e.Handled = true; } } }