AlignmentConverter.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Globalization;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Data.Converters;
  6. using Avalonia.Media.Immutable;
  7. using Dock.Model.Core;
  8. namespace Dock.Avalonia.Converters;
  9. /// <summary>
  10. /// Converts model <see cref="Alignment"/> enum to avalonia <see cref="Dock"/> enum.
  11. /// </summary>
  12. public class AlignmentConverter : IValueConverter
  13. {
  14. /// <summary>
  15. /// Gets <see cref="AlignmentConverter"/> instance.
  16. /// </summary>
  17. public static readonly AlignmentConverter Instance = new AlignmentConverter();
  18. /// <summary>
  19. /// Converts a value.
  20. /// </summary>
  21. /// <param name="value">The value to convert.</param>
  22. /// <param name="targetType">The type of the target.</param>
  23. /// <param name="parameter">A user-defined parameter.</param>
  24. /// <param name="culture">The culture to use.</param>
  25. /// <returns>The converted value.</returns>
  26. public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  27. {
  28. return value switch
  29. {
  30. null => AvaloniaProperty.UnsetValue,
  31. Alignment alignment => alignment switch
  32. {
  33. Alignment.Unset => AvaloniaProperty.UnsetValue,
  34. Alignment.Left => global::Avalonia.Controls.Dock.Left,
  35. Alignment.Bottom => global::Avalonia.Controls.Dock.Bottom,
  36. Alignment.Right => global::Avalonia.Controls.Dock.Right,
  37. Alignment.Top => global::Avalonia.Controls.Dock.Top,
  38. _ => throw new NotSupportedException($"Provided dock is not supported in Avalonia.")
  39. },
  40. _ => value
  41. };
  42. }
  43. /// <summary>
  44. /// Converts a value.
  45. /// </summary>
  46. /// <param name="value">The value to convert.</param>
  47. /// <param name="targetType">The type of the target.</param>
  48. /// <param name="parameter">A user-defined parameter.</param>
  49. /// <param name="culture">The culture to use.</param>
  50. /// <returns>The converted value.</returns>
  51. public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  52. {
  53. return value switch
  54. {
  55. null => AvaloniaProperty.UnsetValue,
  56. global::Avalonia.Controls.Dock dock => dock switch
  57. {
  58. global::Avalonia.Controls.Dock.Left => Alignment.Left,
  59. global::Avalonia.Controls.Dock.Bottom => Alignment.Bottom,
  60. global::Avalonia.Controls.Dock.Right => Alignment.Right,
  61. global::Avalonia.Controls.Dock.Top => Alignment.Top,
  62. _ => Alignment.Unset
  63. },
  64. _ => value
  65. };
  66. }
  67. }
  68. public class TransparentToTrueConverter : IValueConverter
  69. {
  70. public static readonly TransparentToTrueConverter Instance = new();
  71. public object? Convert(object? value, Type targetType, object? parameter,
  72. CultureInfo culture)
  73. {
  74. if (value == null)
  75. return false;
  76. var b = (ImmutableSolidColorBrush)value;
  77. return b.Opacity != 0;
  78. }
  79. public object ConvertBack(object? value, Type targetType,
  80. object? parameter, CultureInfo culture)
  81. {
  82. throw new NotSupportedException();
  83. }
  84. }