OrientationConverter.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Globalization;
  3. using Avalonia;
  4. using Avalonia.Data.Converters;
  5. using Avalonia.Layout;
  6. namespace Dock.Avalonia.Converters;
  7. /// <summary>
  8. /// Converts model <see cref="Model.Core.Orientation"/> enum to avalonia <see cref="Orientation"/> enum.
  9. /// </summary>
  10. public class OrientationConverter : IValueConverter
  11. {
  12. /// <summary>
  13. /// Gets <see cref="OrientationConverter"/> instance.
  14. /// </summary>
  15. public static readonly OrientationConverter Instance = new OrientationConverter();
  16. /// <summary>
  17. /// Converts a value.
  18. /// </summary>
  19. /// <param name="value">The value to convert.</param>
  20. /// <param name="targetType">The type of the target.</param>
  21. /// <param name="parameter">A user-defined parameter.</param>
  22. /// <param name="culture">The culture to use.</param>
  23. /// <returns>The converted value.</returns>
  24. public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  25. {
  26. return value switch
  27. {
  28. null => AvaloniaProperty.UnsetValue,
  29. Model.Core.Orientation orientation => orientation switch
  30. {
  31. Model.Core.Orientation.Horizontal => Orientation.Horizontal,
  32. Model.Core.Orientation.Vertical => Orientation.Vertical,
  33. _ => throw new NotSupportedException($"Provided orientation is not supported in Avalonia.")
  34. },
  35. _ => value
  36. };
  37. }
  38. /// <summary>
  39. /// Converts a value.
  40. /// </summary>
  41. /// <param name="value">The value to convert.</param>
  42. /// <param name="targetType">The type of the target.</param>
  43. /// <param name="parameter">A user-defined parameter.</param>
  44. /// <param name="culture">The culture to use.</param>
  45. /// <returns>The converted value.</returns>
  46. public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  47. {
  48. return value switch
  49. {
  50. null => AvaloniaProperty.UnsetValue,
  51. Orientation orientation => orientation switch
  52. {
  53. Orientation.Horizontal => Model.Core.Orientation.Horizontal,
  54. Orientation.Vertical => Model.Core.Orientation.Vertical,
  55. _ => throw new NotSupportedException($"Provided orientation is not supported in Model.")
  56. },
  57. _ => value
  58. };
  59. }
  60. }