Dispenser/DispenserUI/Views/Windows/UserFormWindow.axaml.cs

80 lines
2.2 KiB
C#

using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using DispenserCore.Model.Entity;
using Masuit.Tools;
namespace DispenserUI.Views.Windows;
public partial class UserFormWindow : Window
{
private TextBox? _nickNameTextBox;
private TextBox? _passwordTextBox;
private Button? _resetPwdTextBtn;
private ComboBox? _roleComboBox;
private readonly User? _user;
private Border? _userForm;
private TextBox? _userNameTextBox;
public UserFormWindow(User? user = null)
{
_user = user;
InitializeComponent();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
_userNameTextBox = this.FindControl<TextBox>("UserName");
_passwordTextBox = this.FindControl<TextBox>("Password");
_nickNameTextBox = this.FindControl<TextBox>("NickName");
_roleComboBox = this.FindControl<ComboBox>("UserRole");
_resetPwdTextBtn = this.FindControl<Button>("ResetPassword");
_userForm = this.FindControl<Border>("UserFormBorder");
if (_user == null) return;
_userNameTextBox!.Text = _user.UserName;
_nickNameTextBox!.Text = _user.NickName;
_passwordTextBox!.Text = _user.Password;
_roleComboBox!.SelectedIndex = _user.Role;
if (_user.Id.NotNullOrEmpty())
{
// 这里是更新的场景,需要显示重置密码按钮
var height = _userForm!.Height;
_userForm.Height = height + 30;
_resetPwdTextBtn!.IsVisible = true;
}
else
{
_resetPwdTextBtn!.IsVisible = true;
_resetPwdTextBtn!.Height = 0;
}
}
private void Confirm(object sender, RoutedEventArgs e)
{
var user = new User
{
Id = _user?.Id,
UserName = _userNameTextBox!.Text,
NickName = _nickNameTextBox!.Text,
Password = _passwordTextBox!.Text,
Role = _roleComboBox!.SelectedIndex
};
Close(user);
}
private void Close(object sender, RoutedEventArgs e)
{
Close();
}
private void ToResetPassword(object sender, RoutedEventArgs e)
{
Close("RESET");
}
}