demo2023/demo_2023/MainWindow.axaml.cs

95 lines
2.5 KiB
C#
Raw Normal View History

2025-01-28 12:20:48 +00:00
using System;
2025-01-22 13:34:31 +00:00
using System.Collections.Generic;
2025-01-28 12:20:48 +00:00
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
2025-01-22 13:34:31 +00:00
using Avalonia.Controls;
using Avalonia.Interactivity;
2025-01-28 12:20:48 +00:00
using Avalonia.Media.Imaging;
2025-01-22 13:34:31 +00:00
using demo_2023.Models;
2025-01-28 18:00:31 +00:00
namespace demo_2023
2025-01-22 13:34:31 +00:00
{
2025-01-28 18:00:31 +00:00
public partial class MainWindow : Window
2025-01-22 13:34:31 +00:00
{
2025-01-29 16:49:00 +00:00
private ObservableCollection<EventPresenter> events = new ObservableCollection<EventPresenter>();
private List<EventPresenter> dataSourceEvent;
private bool isAscending = true; // Флаг для сортировки
2025-01-28 12:20:48 +00:00
2025-01-28 18:00:31 +00:00
public MainWindow()
{
InitializeComponent();
2025-01-28 12:20:48 +00:00
2025-01-28 18:00:31 +00:00
using var context = new User15Context();
2025-01-28 12:20:48 +00:00
2025-01-29 16:49:00 +00:00
dataSourceEvent = context.Events.Select(it => new EventPresenter
2025-01-28 18:00:31 +00:00
{
Sobitie = it.Sobitie,
Date = it.Date,
Days = it.Days,
City = it.City,
Photo = it.Photo
}).ToList();
2025-01-29 16:49:00 +00:00
EventListBox.ItemsSource = events;
ApplySortingAndFiltering();
}
private void ApplySortingAndFiltering()
{
var temp = dataSourceEvent;
if (DateComboBox.SelectedItem is DateTime selectedDateTime)
2025-01-28 18:00:31 +00:00
{
2025-01-29 16:49:00 +00:00
temp = temp.Where(it => it.Date.Date == selectedDateTime.Date).ToList();
2025-01-28 18:00:31 +00:00
}
2025-01-29 16:49:00 +00:00
temp = isAscending ? temp.OrderBy(it => it.Date).ToList() : temp.OrderByDescending(it => it.Date).ToList();
events.Clear();
foreach (var item in temp)
{
events.Add(item);
}
2025-01-28 18:00:31 +00:00
}
public class EventPresenter : Event
2025-01-28 12:20:48 +00:00
{
2025-01-28 18:00:31 +00:00
public Bitmap? Image
2025-01-28 12:20:48 +00:00
{
2025-01-28 18:00:31 +00:00
get
2025-01-28 12:20:48 +00:00
{
2025-01-28 18:00:31 +00:00
try
{
string absolutePath = Path.Combine(AppContext.BaseDirectory, Photo);
2025-01-29 16:49:00 +00:00
return File.Exists(absolutePath) ? new Bitmap(absolutePath) : null;
2025-01-28 18:00:31 +00:00
}
catch
{
2025-01-29 16:49:00 +00:00
return null;
2025-01-28 18:00:31 +00:00
}
2025-01-28 12:20:48 +00:00
}
}
}
2025-01-28 18:00:31 +00:00
2025-01-29 16:49:00 +00:00
private void DateComboBox_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{
isAscending = !isAscending;
ApplySortingAndFiltering();
}
2025-01-28 18:00:31 +00:00
private void Jury_reg(object? sender, RoutedEventArgs e)
{
new Additem().ShowDialog(this);
}
2025-01-22 13:34:31 +00:00
}
2025-01-28 18:00:31 +00:00
}