Flyout.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.ComponentModel;
  2. using Avalonia.Controls.Primitives;
  3. using Avalonia.Metadata;
  4. using Avalonia.Styling;
  5. namespace Avalonia.Controls
  6. {
  7. public class Flyout : PopupFlyoutBase
  8. {
  9. /// <summary>
  10. /// Defines the <see cref="Content"/> property
  11. /// </summary>
  12. public static readonly StyledProperty<object> ContentProperty =
  13. AvaloniaProperty.Register<Flyout, object>(nameof(Content));
  14. private Classes? _classes;
  15. /// <summary>
  16. /// Gets the Classes collection to apply to the FlyoutPresenter this Flyout is hosting
  17. /// </summary>
  18. public Classes FlyoutPresenterClasses
  19. {
  20. get => _classes ??= new Classes();
  21. set
  22. {
  23. if (_classes is null)
  24. _classes = value;
  25. else if (_classes != value)
  26. _classes.Replace(value);
  27. }
  28. }
  29. /// <summary>
  30. /// Defines the <see cref="FlyoutPresenterTheme"/> property.
  31. /// </summary>
  32. public static readonly StyledProperty<ControlTheme?> FlyoutPresenterThemeProperty =
  33. AvaloniaProperty.Register<Flyout, ControlTheme?>(nameof(FlyoutPresenterTheme));
  34. /// <summary>
  35. /// Gets or sets the <see cref="ControlTheme"/> that is applied to the container element generated for the flyout presenter.
  36. /// </summary>
  37. public ControlTheme? FlyoutPresenterTheme
  38. {
  39. get => GetValue(FlyoutPresenterThemeProperty);
  40. set => SetValue(FlyoutPresenterThemeProperty, value);
  41. }
  42. /// <summary>
  43. /// Gets or sets the content to display in this flyout
  44. /// </summary>
  45. [Content]
  46. public object Content
  47. {
  48. get => GetValue(ContentProperty);
  49. set => SetValue(ContentProperty, value);
  50. }
  51. protected override Control CreatePresenter()
  52. {
  53. return new FlyoutPresenter
  54. {
  55. [!ContentControl.ContentProperty] = this[!ContentProperty]
  56. };
  57. }
  58. protected override void OnOpening(CancelEventArgs args)
  59. {
  60. if (Popup.Child is { } presenter)
  61. {
  62. if (_classes != null)
  63. {
  64. SetPresenterClasses(presenter, FlyoutPresenterClasses);
  65. }
  66. if (FlyoutPresenterTheme is { } theme)
  67. {
  68. presenter.SetValue(Control.ThemeProperty, theme);
  69. }
  70. }
  71. base.OnOpening(args);
  72. }
  73. }
  74. }