Presence.Desktop/Presence.Desktop/Views/EditUserDialog.axaml.cs
2024-12-20 11:49:17 +03:00

67 lines
2.2 KiB
C#

using Avalonia.Controls;
using Avalonia.Controls.Templates;
using data.RemoteData.RemoteDataBase.DAO;
using Presence.Desktop.ViewModels;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using data.RemoteData.RemoteDataBase.DAO;
namespace Presence.Desktop
{
public partial class EditUserDialog : Window
{
private TextBox _nameTextBox;
private ComboBox _groupComboBox;
// Êîíñòðóêòîð, ïðèíèìàþùèé èìÿ è ID ãðóïïû
public EditUserDialog(int currentUserId, string currentName, int currentGroupId, List<data.RemoteData.RemoteDataBase.DAO.GroupDao> groups)
{
_nameTextBox = new TextBox { Text = currentName };
_groupComboBox = new ComboBox
{
ItemsSource = groups, // Óñòàíàâëèâàåì èñòî÷íèê äàííûõ
SelectedItem = groups.FirstOrDefault(g => g.Id == currentGroupId), // Óñòàíàâëèâàåì òåêóùóþ ãðóïïó
ItemTemplate = new FuncDataTemplate<data.RemoteData.RemoteDataBase.DAO.GroupDao>((group, _) =>
new TextBlock { Text = group.Name }) // Îòîáðàæàåì èìÿ ãðóïïû
};
var confirmButton = new Button { Content = "OK" };
confirmButton.Click += (sender, args) =>
{
var newFio = _nameTextBox.Text;
var selectedGroup = (data.RemoteData.RemoteDataBase.DAO.GroupDao)_groupComboBox.SelectedItem;
if (selectedGroup != null)
{
var newGroupId = selectedGroup.Id; // Ïîëó÷àåì ID âûáðàííîé ãðóïïû
// Çàêðûâàåì äèàëîã
this.Close();
}
};
Content = new StackPanel
{
Children = { _nameTextBox, _groupComboBox, confirmButton }
};
}
// Àñèíõðîííûé ìåòîä äëÿ ïîëó÷åíèÿ ðåçóëüòàòà îò ïîëüçîâàòåëÿ
public async Task<(string, GroupDao)> ShowEditDialog(Window parent)
{
await base.ShowDialog(parent); // Ïåðåäàåì ðîäèòåëüñêîå îêíî
var name = _nameTextBox.Text;
var groupId = (GroupDao)_groupComboBox.SelectedItem;
return (name, groupId);
}
}
}