-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
MenuFlyout.cs
121 lines (103 loc) · 4.01 KB
/
MenuFlyout.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System.Collections;
using System.ComponentModel;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Metadata;
using Avalonia.Styling;
namespace Avalonia.Controls
{
public class MenuFlyout : PopupFlyoutBase
{
public MenuFlyout()
{
Items = new ItemCollection();
}
/// <summary>
/// Defines the <see cref="ItemsSource"/> property
/// </summary>
public static readonly StyledProperty<IEnumerable?> ItemsSourceProperty =
AvaloniaProperty.Register<MenuFlyout, IEnumerable?>(
nameof(ItemsSource));
/// <summary>
/// Defines the <see cref="ItemTemplate"/> property
/// </summary>
public static readonly StyledProperty<IDataTemplate?> ItemTemplateProperty =
AvaloniaProperty.Register<MenuFlyout, IDataTemplate?>(nameof(ItemTemplate));
/// <summary>
/// Defines the <see cref="ItemContainerTheme"/> property.
/// </summary>
public static readonly StyledProperty<ControlTheme?> ItemContainerThemeProperty =
ItemsControl.ItemContainerThemeProperty.AddOwner<MenuFlyout>();
/// <summary>
/// Defines the <see cref="FlyoutPresenterTheme"/> property.
/// </summary>
public static readonly StyledProperty<ControlTheme?> FlyoutPresenterThemeProperty =
Flyout.FlyoutPresenterThemeProperty.AddOwner<MenuFlyout>();
public Classes FlyoutPresenterClasses => _classes ??= new Classes();
[Content]
public ItemCollection Items { get; }
/// <summary>
/// Gets or sets the items of the MenuFlyout
/// </summary>
public IEnumerable? ItemsSource
{
get => GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
/// <summary>
/// Gets or sets the template used for the items
/// </summary>
public IDataTemplate? ItemTemplate
{
get => GetValue(ItemTemplateProperty);
set => SetValue(ItemTemplateProperty, value);
}
/// <summary>
/// Gets or sets the <see cref="ControlTheme"/> that is applied to the container element generated for each item.
/// </summary>
public ControlTheme? ItemContainerTheme
{
get => GetValue(ItemContainerThemeProperty);
set => SetValue(ItemContainerThemeProperty, value);
}
/// <summary>
/// Gets or sets the <see cref="ControlTheme"/> that is applied to the container element generated for the flyout presenter.
/// </summary>
public ControlTheme? FlyoutPresenterTheme
{
get => GetValue(FlyoutPresenterThemeProperty);
set => SetValue(FlyoutPresenterThemeProperty, value);
}
private Classes? _classes;
protected override Control CreatePresenter()
{
return new MenuFlyoutPresenter
{
ItemsSource = Items,
[!ItemsControl.ItemTemplateProperty] = this[!ItemTemplateProperty],
[!ItemsControl.ItemContainerThemeProperty] = this[!ItemContainerThemeProperty],
};
}
protected override void OnOpening(CancelEventArgs args)
{
if (Popup.Child is { } presenter)
{
if (_classes != null)
{
SetPresenterClasses(presenter, FlyoutPresenterClasses);
}
if (FlyoutPresenterTheme is { } theme)
{
presenter.SetValue(Control.ThemeProperty, theme);
}
}
base.OnOpening(args);
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == ItemsSourceProperty)
Items.SetItemsSource(change.GetNewValue<IEnumerable?>());
}
}
}