using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Media; using Avalonia.Media.Imaging; using Avalonia.Platform; using Avalonia.Platform.Storage; using Demo.EntityModels; using Demo.Utils; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Threading.Tasks; using Tmds.DBus.Protocol; namespace Demo { public partial class MainWindow : Window { //flags private bool AdminMode = false; private bool IsFiltred = false; //null - nothing, 0 - asc, 1 - desc private bool? SortedMode = null; //changedList private string SearchWord = ""; //constants for comboboxes private string[] sortValues = new string[3] { "отсут", "по возраст.", "по убыв." }; private List services = new List() { new Service(){ ServiceName = "Abc", ServiceDiscount="Abc", ServicePriceMinutes = "Abc" }, new Service(){ ServiceName = "cba", ServiceDiscount="cba", ServicePriceMinutes = "cba" }, new Service(){ ServiceName = "bca", ServiceDiscount="bca", ServicePriceMinutes = "bca" }, new Service(){ ServiceName = "ccb", ServiceDiscount="ccb", ServicePriceMinutes = "ccb" } }; public MainWindow() { InitializeComponent(); MyImage.Source = new Bitmap(AssetLoader.Open(new System.Uri("avares://Assets/service_layout.png"))); services = Context .DbContext .Services.Select(service => new Service { ServiceDiscount = service.Discount.ToString(), ServiceName = service.Title, ServicePriceMinutes = $"{service.Cost}/{service.Durationinminutes}" }).ToList(); SortComboBox.ItemsSource = sortValues; SortComboBox.SelectedIndex = 0; DisplayService(); } private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e) { AdminPanel.IsVisible = !AdminPanel.IsVisible; } private void SearchTextBox_TextChanging(object? sender, Avalonia.Controls.TextChangingEventArgs e) { string inputValue = (sender as TextBox).Text; SearchWord = inputValue.ToLower(); DisplayService(); } public async void ChangeModeButton_ClickAsync(object sender, RoutedEventArgs e) { var changeModeDialog = new ChangeModeDialog(); AdminMode = await changeModeDialog.ShowDialog(this); AdminPanel.IsVisible = AdminMode; DisplayService(); } private void DisplayService() { List displayList = services; if (!string.IsNullOrEmpty(SearchWord)) { displayList = displayList .Where(x => x.ServiceName .ToLower() .Contains(SearchWord) ).ToList(); } switch (SortedMode) { case true: displayList = displayList .OrderBy( service => service.ServiceName) .ToList(); break; case false: displayList = displayList .OrderByDescending( service => service.ServiceName) .ToList(); break; case null: break; } ServiceList.ItemsSource = displayList .Select(service => service.SetAdminMode(AdminMode)) .ToList(); CountValues.Text = $"Отобржается:{displayList.Count}/Всего:{services.Count}"; } class Service() { public string ServiceName { get; set; } public string ServicePriceMinutes { get; set; } public string ServiceDiscount { get; set; } public bool AdminMode { get; set; } = false; public Service SetAdminMode(bool mode) { this.AdminMode = mode; return this; } } private void SortComboBox_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e) { int selectedIndex = (sender as ComboBox).SelectedIndex; switch (selectedIndex) { case 1: SortedMode = false; break; case 2: SortedMode = true; break; default: SortedMode = null; break; } DisplayService(); } } }