ContentExpandControl.axaml.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Layout;
  5. namespace SukiUI.Controls
  6. {
  7. public class ContentExpandControl : ContentControl
  8. {
  9. public static readonly StyledProperty<double> MultiplierProperty =
  10. AvaloniaProperty.Register<ContentExpandControl, double>(nameof(Multiplier));
  11. public double Multiplier
  12. {
  13. get => GetValue(MultiplierProperty);
  14. set => SetValue(MultiplierProperty, value);
  15. }
  16. public static readonly StyledProperty<Orientation> OrientationProperty =
  17. AvaloniaProperty.Register<ContentExpandControl, Orientation>(nameof(Orientation));
  18. public Orientation Orientation
  19. {
  20. get => GetValue(OrientationProperty);
  21. set => SetValue(OrientationProperty, value);
  22. }
  23. static ContentExpandControl()
  24. {
  25. AffectsArrange<ContentExpandControl>(MultiplierProperty, OrientationProperty);
  26. AffectsMeasure<ContentExpandControl>(MultiplierProperty, OrientationProperty);
  27. }
  28. protected override Size MeasureCore(Size availableSize)
  29. {
  30. var result = base.MeasureCore(availableSize);
  31. return result;
  32. }
  33. protected override Size ArrangeOverride(Size finalSize)
  34. {
  35. var result = base.ArrangeOverride(finalSize);
  36. // A very gross way to actually get the bloody thing to draw...
  37. if (Parent is Control c) c.Margin = new Thickness(1);
  38. return result;
  39. }
  40. protected override Size MeasureOverride(Size availableSize)
  41. {
  42. var result = base.MeasureOverride(availableSize);
  43. var w = result.Width;
  44. var h = result.Height;
  45. switch (Orientation)
  46. {
  47. case Orientation.Horizontal:
  48. w *= Multiplier;
  49. break;
  50. case Orientation.Vertical:
  51. h *= Multiplier;
  52. break;
  53. default:
  54. throw new ArgumentOutOfRangeException();
  55. }
  56. // A very gross way to actually get the bloody thing to draw...
  57. if (Parent is Control c) c.Margin = new Thickness(0);
  58. return new Size(w, h);
  59. }
  60. }
  61. }