1
- using CommunityToolkit . Mvvm . Input ;
2
- using System ;
1
+ using System ;
3
2
using System . Collections . Generic ;
3
+ using System . Windows . Input ;
4
4
using Windows . UI . Xaml ;
5
5
using Windows . UI . Xaml . Controls ;
6
6
@@ -10,50 +10,55 @@ public class MenuFlyoutHelper : DependencyObject
10
10
{
11
11
#region View Models
12
12
13
- public interface IMenuFlyoutItem { }
13
+ public interface IMenuFlyoutItemViewModel { }
14
14
15
- public class MenuFlyoutSeparatorViewModel : IMenuFlyoutItem { }
15
+ public class MenuFlyoutSeparatorViewModel : IMenuFlyoutItemViewModel { }
16
16
17
- public abstract class MenuFlyoutItemBaseViewModel : IMenuFlyoutItem
17
+ public class MenuFlyoutItemViewModel : IMenuFlyoutItemViewModel
18
18
{
19
- public string Text { get ; }
19
+ public string Text { get ; init ; }
20
+
21
+ public ICommand Command { get ; init ; }
22
+
23
+ public object CommandParameter { get ; init ; }
24
+
25
+ public string Tooltip { get ; init ; }
20
26
21
27
public bool IsEnabled { get ; set ; } = true ;
22
28
23
- internal MenuFlyoutItemBaseViewModel ( string text ) => Text = text ;
29
+ public MenuFlyoutItemViewModel ( string text )
30
+ => Text = text ;
24
31
}
25
32
26
- public class MenuFlyoutItemViewModel : MenuFlyoutItemBaseViewModel
33
+ public class MenuFlyoutSubItemViewModel : IMenuFlyoutItemViewModel
27
34
{
28
- public string Path { get ; }
35
+ public string Text { get ; }
29
36
30
- public RelayCommand < string > OnSelect { get ; }
37
+ public bool IsEnabled { get ; set ; } = true ;
31
38
32
- internal MenuFlyoutItemViewModel ( string text , string path , RelayCommand < string > onSelect ) : base ( text )
33
- {
34
- Path = path ;
35
- OnSelect = onSelect ;
36
- }
39
+ public IList < IMenuFlyoutItemViewModel > Items { get ; } = new List < IMenuFlyoutItemViewModel > ( ) ;
40
+
41
+ public MenuFlyoutSubItemViewModel ( string text )
42
+ => Text = text ;
37
43
}
38
44
39
- public class MenuFlyoutSubItemViewModel : MenuFlyoutItemBaseViewModel
45
+ public class MenuFlyoutFactoryItemViewModel : IMenuFlyoutItemViewModel
40
46
{
41
- public IList < IMenuFlyoutItem > Items { get ; } = new List < IMenuFlyoutItem > ( ) ;
47
+ public Func < MenuFlyoutItemBase > Build { get ; }
42
48
43
- internal MenuFlyoutSubItemViewModel ( string text ) : base ( text )
44
- {
45
- }
49
+ public MenuFlyoutFactoryItemViewModel ( Func < MenuFlyoutItemBase > factoryFunc )
50
+ => Build = factoryFunc ;
46
51
}
47
52
48
53
#endregion View Models
49
54
50
55
#region ItemsSource
51
56
52
- public static IEnumerable < IMenuFlyoutItem > GetItemsSource ( DependencyObject obj ) => obj . GetValue ( ItemsSourceProperty ) as IEnumerable < IMenuFlyoutItem > ;
57
+ public static IEnumerable < IMenuFlyoutItemViewModel > GetItemsSource ( DependencyObject obj ) => obj . GetValue ( ItemsSourceProperty ) as IEnumerable < IMenuFlyoutItemViewModel > ;
53
58
54
- public static void SetItemsSource ( DependencyObject obj , IEnumerable < IMenuFlyoutItem > value ) => obj . SetValue ( ItemsSourceProperty , value ) ;
59
+ public static void SetItemsSource ( DependencyObject obj , IEnumerable < IMenuFlyoutItemViewModel > value ) => obj . SetValue ( ItemsSourceProperty , value ) ;
55
60
56
- public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty . RegisterAttached ( "ItemsSource" , typeof ( IEnumerable < IMenuFlyoutItem > ) , typeof ( MenuFlyoutHelper ) , new PropertyMetadata ( null , ItemsSourceChanged ) ) ;
61
+ public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty . RegisterAttached ( "ItemsSource" , typeof ( IEnumerable < IMenuFlyoutItemViewModel > ) , typeof ( MenuFlyoutHelper ) , new PropertyMetadata ( null , ItemsSourceChanged ) ) ;
57
62
58
63
private static void ItemsSourceChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e ) => SetupItems ( d as MenuFlyout ) ;
59
64
@@ -102,7 +107,7 @@ await menu.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, ()
102
107
} ) ;
103
108
}
104
109
105
- private static void AddItems ( IList < MenuFlyoutItemBase > menu , IEnumerable < IMenuFlyoutItem > items )
110
+ private static void AddItems ( IList < MenuFlyoutItemBase > menu , IEnumerable < IMenuFlyoutItemViewModel > items )
106
111
{
107
112
foreach ( var item in items )
108
113
{
@@ -115,13 +120,13 @@ private static void AddItems(IList<MenuFlyoutItemBase> menu, IEnumerable<IMenuFl
115
120
var mfi = new MenuFlyoutItem
116
121
{
117
122
Text = vm . Text ,
118
- Command = vm . OnSelect ,
119
- CommandParameter = vm . Path ,
123
+ Command = vm . Command ,
124
+ CommandParameter = vm . CommandParameter ,
120
125
IsEnabled = vm . IsEnabled ,
121
126
} ;
122
- if ( ! string . IsNullOrEmpty ( vm . Path ) )
127
+ if ( ! string . IsNullOrEmpty ( vm . Tooltip ) )
123
128
{
124
- ToolTipService . SetToolTip ( mfi , vm . Path ) ;
129
+ ToolTipService . SetToolTip ( mfi , vm . Tooltip ) ;
125
130
}
126
131
menu . Add ( mfi ) ;
127
132
}
@@ -135,6 +140,10 @@ private static void AddItems(IList<MenuFlyoutItemBase> menu, IEnumerable<IMenuFl
135
140
AddItems ( mfsi . Items , svm . Items ) ;
136
141
menu . Add ( mfsi ) ;
137
142
}
143
+ else if ( item is MenuFlyoutFactoryItemViewModel fvm )
144
+ {
145
+ menu . Add ( fvm . Build ( ) ) ;
146
+ }
138
147
}
139
148
}
140
149
}
0 commit comments