DockModeConverter.cs 2.3 KB

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