DynamicRowMovementColumn.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Linq;
  3. using System.Windows.Media.Imaging;
  4. using InABox.Core;
  5. using InABox.WPF;
  6. namespace InABox.DynamicGrid
  7. {
  8. public enum DynamicRowMovement
  9. {
  10. Up,
  11. Down
  12. }
  13. public class DynamicRowMovementColumn : DynamicImageColumn
  14. {
  15. private readonly BitmapImage downarrow =Wpf.Resources.downarrow.AsBitmapImage();
  16. private readonly Func<int, int, bool> Swap;
  17. private readonly BitmapImage uparrow =Wpf.Resources.uparrow.AsBitmapImage();
  18. public DynamicRowMovementColumn(DynamicRowMovement direction, Func<int, int, bool> swap) : base(r => null)
  19. {
  20. Direction = direction;
  21. Swap = swap;
  22. Image = GetImage;
  23. Action = MoveRow;
  24. Position = DynamicActionColumnPosition.Start;
  25. }
  26. public DynamicRowMovement Direction { get; }
  27. private static bool IsFirst(CoreRow? row)
  28. {
  29. return row != null && row.Table.Rows.First() == row; // (row.Index == 0);
  30. }
  31. private static bool IsLast(CoreRow? row)
  32. {
  33. return row != null && row.Table.Rows.Last() == row; //(row.Index == row.Table.Rows.Count - 1);
  34. }
  35. private BitmapImage? GetImage(CoreRow? row)
  36. {
  37. if (Direction.Equals(DynamicRowMovement.Up))
  38. return IsFirst(row) ? null : uparrow;
  39. return IsLast(row) ? null : downarrow;
  40. }
  41. private bool MoveRow(CoreRow? row)
  42. {
  43. if (row is null) return false;
  44. var tgt = row.Index + (Direction.Equals(DynamicRowMovement.Up) ? -1 : 1);
  45. if (tgt > -1 && tgt < row.Table.Rows.Count)
  46. return Swap(row.Index, tgt);
  47. return false;
  48. }
  49. }
  50. }