DynamicRowMovementColumn.cs 1.7 KB

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