12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using InABox.Core;
- using InABox.WPF;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Controls;
- using System.Windows.Media.Imaging;
- namespace InABox.DynamicGrid;
- public class DynamicSelectorGrid<T> : DynamicDataGrid<T>
- where T : Entity, IRemotable, IPersistent, new()
- {
- private static BitmapImage tick = InABox.Wpf.Resources.tick.AsBitmapImage();
- public HashSet<Guid> SelectedIDs { get; set; } = new();
- public delegate void SelectionChangedEvent(HashSet<Guid> selected);
- public event SelectionChangedEvent? SelectionChanged;
- public DynamicSelectorGrid(DynamicActionColumnPosition tickPosition)
- {
- ActionColumns.Add(new DynamicImageColumn(Selected_Image, Selected_Click)
- {
- Position = tickPosition
- });
- }
- private BitmapImage? Selected_Image(CoreRow? row)
- {
- if (row is null) return tick;
- return SelectedIDs.Contains(row.Get<T, Guid>(x => x.ID)) ? tick : null;
- }
- private bool Selected_Click(CoreRow? row)
- {
- if (row is null)
- {
- var menu = new ContextMenu();
- menu.AddItem("Select All", null, SelectAll_Click);
- menu.AddItem("Deselect All", null, DeselectAll_Click);
- menu.IsOpen = true;
- return false;
- }
- var id = row.Get<T, Guid>(x => x.ID);
- if (SelectedIDs.Contains(id))
- {
- SelectedIDs.Remove(id);
- }
- else
- {
- SelectedIDs.Add(id);
- }
- SelectionChanged?.Invoke(SelectedIDs);
- InvalidateRow(row);
- return false;
- }
- private void DeselectAll_Click()
- {
- SelectedIDs.Clear();
- SelectionChanged?.Invoke(SelectedIDs);
- InvalidateGrid();
- }
- private void SelectAll_Click()
- {
- SelectedIDs = Data.Rows.Select(x => x.Get<T, Guid>(x => x.ID)).ToHashSet();
- SelectionChanged?.Invoke(SelectedIDs);
- InvalidateGrid();
- }
- protected override void DoReconfigure(FluentList<DynamicGridOption> options)
- {
- base.DoReconfigure(options);
- options.Clear();
- }
- }
|