69 lines
2.2 KiB
C#
69 lines
2.2 KiB
C#
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.ComponentModel;
|
|
using System.Windows.Threading;
|
|
using UtopiaCanteenSystem.ViewModels;
|
|
|
|
namespace UtopiaCanteenSystem.Views;
|
|
|
|
/// <summary>
|
|
/// Minimal code-behind for PasswordBox + initial focus.
|
|
/// </summary>
|
|
public partial class AdminLoginView : UserControl
|
|
{
|
|
private AdminLoginViewModel? _vm;
|
|
|
|
public AdminLoginView()
|
|
{
|
|
InitializeComponent();
|
|
Loaded += OnLoaded;
|
|
DataContextChanged += OnDataContextChanged;
|
|
}
|
|
|
|
private void OnLoaded(object sender, RoutedEventArgs e)
|
|
{
|
|
Dispatcher.BeginInvoke(() =>
|
|
{
|
|
// Ensure stale PasswordBox contents never carry across sessions (PasswordBox isn't bindable).
|
|
PasswordBox.Password = string.Empty;
|
|
if (DataContext is AdminLoginViewModel vm)
|
|
vm.Password = string.Empty;
|
|
|
|
UsernameTextBox.Focus();
|
|
UsernameTextBox.SelectAll();
|
|
}, DispatcherPriority.Input);
|
|
}
|
|
|
|
private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
if (_vm != null)
|
|
_vm.PropertyChanged -= VmOnPropertyChanged;
|
|
|
|
_vm = DataContext as AdminLoginViewModel;
|
|
if (_vm != null)
|
|
_vm.PropertyChanged += VmOnPropertyChanged;
|
|
|
|
// When we navigate back here after logout, the view might be reused; always clear password UI.
|
|
PasswordBox.Password = string.Empty;
|
|
if (DataContext is AdminLoginViewModel vm)
|
|
vm.Password = string.Empty;
|
|
}
|
|
|
|
private void VmOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
|
|
{
|
|
if (!string.Equals(e.PropertyName, nameof(AdminLoginViewModel.Password), StringComparison.Ordinal))
|
|
return;
|
|
|
|
// Keep PasswordBox UI in sync when VM clears password after clicking Login.
|
|
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 AdminLoginViewModel vm)
|
|
vm.Password = PasswordBox.Password;
|
|
}
|
|
}
|
|
|