File tree Expand file tree Collapse file tree 2 files changed +50
-1
lines changed
tests/Avalonia.Controls.UnitTests Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Collections . Specialized ;
3
4
using System . Linq ;
4
5
using System . Windows . Input ;
5
6
using Avalonia . Automation ;
@@ -340,7 +341,7 @@ public string? GroupName
340
341
/// <inheritdoc/>
341
342
IMenuElement ? IMenuItem . Parent => Parent as IMenuElement ;
342
343
343
- protected override bool IsEnabledCore => base . IsEnabledCore && _commandCanExecute ;
344
+ protected override bool IsEnabledCore => base . IsEnabled && ( HasSubMenu || _commandCanExecute ) ;
344
345
345
346
/// <inheritdoc/>
346
347
bool IMenuElement . MoveSelection ( NavigationDirection direction , bool wrap ) => MoveSelection ( direction , wrap ) ;
@@ -700,6 +701,15 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
700
701
{
701
702
GroupNameChanged ( change ) ;
702
703
}
704
+ else if ( change . Property == ItemCountProperty )
705
+ {
706
+ // A menu item with no sub-menu is effectively disabled if its command binding
707
+ // failed: this means that the effectively enabled state depends on whether the
708
+ // number of items in the menu is 0 or not.
709
+ var ( o , n ) = change . GetOldAndNewValue < int > ( ) ;
710
+ if ( o == 0 || n == 0 )
711
+ UpdateIsEffectivelyEnabled ( ) ;
712
+ }
703
713
}
704
714
/// <summary>
705
715
/// Called when the <see cref="GroupName"/> property changes.
Original file line number Diff line number Diff line change @@ -62,6 +62,45 @@ public void MenuItem_Is_Disabled_When_Bound_Command_Doesnt_Exist()
62
62
Assert . False ( target . IsEffectivelyEnabled ) ;
63
63
}
64
64
65
+ [ Fact ]
66
+ public void MenuItem_With_Styled_Command_Binding_Should_Be_Enabled_With_Child_Missing_Command ( )
67
+ {
68
+ using var app = Application ( ) ;
69
+
70
+ var viewModel = new MenuViewModel ( "Parent" )
71
+ {
72
+ Children = [ new MenuViewModel ( "Child" ) ]
73
+ } ;
74
+
75
+ var contextMenu = new ContextMenu
76
+ {
77
+ ItemsSource = new [ ] { viewModel } ,
78
+ Styles =
79
+ {
80
+ new Style ( x => x . OfType < MenuItem > ( ) )
81
+ {
82
+ Setters =
83
+ {
84
+ new Setter ( MenuItem . HeaderProperty , new Binding ( "Header" ) ) ,
85
+ new Setter ( MenuItem . ItemsSourceProperty , new Binding ( "Children" ) ) ,
86
+ new Setter ( MenuItem . CommandProperty , new Binding ( "Command" ) )
87
+ }
88
+ }
89
+ }
90
+ } ;
91
+
92
+ var window = new Window { ContextMenu = contextMenu } ;
93
+ window . Show ( ) ;
94
+ contextMenu . Open ( ) ;
95
+
96
+ var parentMenuItem = Assert . IsType < MenuItem > ( contextMenu . ContainerFromIndex ( 0 ) ) ;
97
+
98
+ Assert . Same ( parentMenuItem . DataContext , viewModel ) ;
99
+ Assert . Same ( parentMenuItem . ItemsSource , viewModel . Children ) ;
100
+ Assert . True ( parentMenuItem . IsEnabled ) ;
101
+ Assert . True ( parentMenuItem . IsEffectivelyEnabled ) ;
102
+ }
103
+
65
104
[ Fact ]
66
105
public void MenuItem_Is_Disabled_When_Bound_Command_Is_Removed ( )
67
106
{
You can’t perform that action at this time.
0 commit comments