Col.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using HandyControl.Data;
  5. using HandyControl.Tools.Extension;
  6. namespace HandyControl.Controls;
  7. public class Col : ContentControl
  8. {
  9. public static readonly DependencyProperty LayoutProperty = DependencyProperty.Register(
  10. nameof(Layout), typeof(ColLayout), typeof(Col), new FrameworkPropertyMetadata(default(ColLayout), FrameworkPropertyMetadataOptions.AffectsParentMeasure));
  11. public ColLayout Layout
  12. {
  13. get => (ColLayout) GetValue(LayoutProperty);
  14. set => SetValue(LayoutProperty, value);
  15. }
  16. public static readonly DependencyProperty OffsetProperty = DependencyProperty.Register(
  17. nameof(Offset), typeof(int), typeof(Col), new FrameworkPropertyMetadata(ValueBoxes.Int0Box, FrameworkPropertyMetadataOptions.AffectsParentMeasure));
  18. public int Offset
  19. {
  20. get => (int) GetValue(OffsetProperty);
  21. set => SetValue(OffsetProperty, value);
  22. }
  23. public static readonly DependencyProperty SpanProperty = DependencyProperty.Register(
  24. nameof(Span), typeof(int), typeof(Col), new FrameworkPropertyMetadata(ColLayout.ColMaxCellCount, FrameworkPropertyMetadataOptions.AffectsParentMeasure), OnSpanValidate);
  25. private static bool OnSpanValidate(object value)
  26. {
  27. var v = (int) value;
  28. return v is >= 1 and <= ColLayout.ColMaxCellCount;
  29. }
  30. public int Span
  31. {
  32. get => (int) GetValue(SpanProperty);
  33. set => SetValue(SpanProperty, value);
  34. }
  35. public static readonly DependencyProperty IsFixedProperty = DependencyProperty.Register(
  36. nameof(IsFixed), typeof(bool), typeof(Col), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.AffectsParentMeasure));
  37. public bool IsFixed
  38. {
  39. get => (bool) GetValue(IsFixedProperty);
  40. set => SetValue(IsFixedProperty, ValueBoxes.BooleanBox(value));
  41. }
  42. internal int GetLayoutCellCount(ColLayoutStatus status)
  43. {
  44. if (Layout is not null)
  45. {
  46. return status switch
  47. {
  48. ColLayoutStatus.Xs => Layout.Xs,
  49. ColLayoutStatus.Sm => Layout.Sm,
  50. ColLayoutStatus.Md => Layout.Md,
  51. ColLayoutStatus.Lg => Layout.Lg,
  52. ColLayoutStatus.Xl => Layout.Xl,
  53. ColLayoutStatus.Xxl => Layout.Xxl,
  54. ColLayoutStatus.Auto => 0,
  55. _ => throw new ArgumentOutOfRangeException(nameof(status), status, null),
  56. };
  57. }
  58. if (IsFixed)
  59. {
  60. return 0;
  61. }
  62. return Span;
  63. }
  64. }