Repository navigation
Conversation
A NativeMenuItem keeps the CanExecute result from the moment its Command is set. When CommandParameter is set or cleared afterwards, IsEnabled does not change. Add tests that set and clear CommandParameter after Command. Both fail at present. A third test records that a parameter change does not enable an item that has no command.
A NativeMenuItem queries CanExecute only when its Command changes. If CommandParameter is assigned afterwards, IsEnabled keeps the result for the previous parameter. This occurs in XAML when the data context is set before the XAML is loaded. The Command binding resolves when it is applied, which is before a CommandParameter property element is assigned. A command that rejects a null parameter, such as RelayCommand<int>, then leaves the item disabled permanently. Query CanExecute again when CommandParameter changes, as Button and MenuItem do. Skip the query when the item has no command, because there is no result to refresh and the query would reset IsEnabled to true. Fixes AvaloniaUI#21914
NathanDrake2406
marked this pull request as ready for review
October 2, 2026 02:06
NuGet packagesThe packages for this pull request have been published (workflow run):
|
maxkatz6
approved these changes
Oct 4, 2026
MrJul
enabled auto-merge
October 5, 2026 16:42
grokys
pushed a commit
to Evan260/Avalonia
that referenced
this pull request
Oct 7, 2026
…aloniaUI#22368) * test(menu): cover NativeMenuItem command parameter changes A NativeMenuItem keeps the CanExecute result from the moment its Command is set. When CommandParameter is set or cleared afterwards, IsEnabled does not change. Add tests that set and clear CommandParameter after Command. Both fail at present. A third test records that a parameter change does not enable an item that has no command. * fix(menu): update NativeMenuItem state when CommandParameter changes A NativeMenuItem queries CanExecute only when its Command changes. If CommandParameter is assigned afterwards, IsEnabled keeps the result for the previous parameter. This occurs in XAML when the data context is set before the XAML is loaded. The Command binding resolves when it is applied, which is before a CommandParameter property element is assigned. A command that rejects a null parameter, such as RelayCommand<int>, then leaves the item disabled permanently. Query CanExecute again when CommandParameter changes, as Button and MenuItem do. Skip the query when the item has no command, because there is no result to refresh and the query would reset IsEnabled to true. Fixes AvaloniaUI#21914 * chore: trim code comments --------- Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
MrJul
added a commit
that referenced
this pull request
Oct 9, 2026
…2368) * test(menu): cover NativeMenuItem command parameter changes A NativeMenuItem keeps the CanExecute result from the moment its Command is set. When CommandParameter is set or cleared afterwards, IsEnabled does not change. Add tests that set and clear CommandParameter after Command. Both fail at present. A third test records that a parameter change does not enable an item that has no command. * fix(menu): update NativeMenuItem state when CommandParameter changes A NativeMenuItem queries CanExecute only when its Command changes. If CommandParameter is assigned afterwards, IsEnabled keeps the result for the previous parameter. This occurs in XAML when the data context is set before the XAML is loaded. The Command binding resolves when it is applied, which is before a CommandParameter property element is assigned. A command that rejects a null parameter, such as RelayCommand<int>, then leaves the item disabled permanently. Query CanExecute again when CommandParameter changes, as Button and MenuItem do. Skip the query when the item has no command, because there is no result to refresh and the query would reset IsEnabled to true. Fixes #21914 * chore: trim code comments --------- Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does the pull request do?
Makes
NativeMenuItemqueryCanExecuteagain when itsCommandParameterchanges, soIsEnabledmatches the current parameter.What is the current behavior?
NativeMenuItemqueriesCommand.CanExecute(CommandParameter)only whenCommandchanges. IfCommandParameteris assigned afterwards,IsEnabledkeeps the result for the previous parameter.This is the cause of #21914. When the application
DataContextis set beforeAvaloniaXamlLoader.Load, aCommandbinding resolves at the moment it is applied. ACommandParameterwritten as a property element is assigned after that:The item calls
CanExecute(null). A command such asRelayCommand<int>returnsfalsefor a null parameter, and the item stays disabled. When theDataContextis set after the XAML is loaded, the parameter is already present when the command arrives, so the item is enabled.A
stringparameter is not affected, becauseRelayCommand<string>acceptsnull.What is the updated/expected behavior with this PR?
Commandset, thenCommandParameterset to a value the command acceptsCommandParameterchanged to a value the command rejectsCommandParameterchanged on an item without aCommandIsEnabledunchangedIsEnabledunchangedIn the sample from the issue, the
Int ParameterandBool Parameteritems are now enabled for all threeDataContextlocations. TheTimespan Parameteritem stays disabled, because its command usesCanNeverExecute.How was the solution implemented (if it's not obvious)?
NativeMenuItem.OnPropertyChangednow calls the existingCanExecuteChanged()method whenCommandParameterPropertychanges.ButtonandMenuItemalready queryCanExecuteagain in theirCommandParameterPropertybranches, so this follows the same contract.The new branch does nothing when
Commandis null.CanExecuteChanged()writesIsEnableddirectly and usestruewhen there is no command. Without the guard, a parameter change would enable an item that hasIsEnabled="False"and no command.Scope:
TrayIconalso hasCommandandCommandParameter, but it queriesCanExecuteonly at click time and stores no result. It is not affected.NativeMenuBarPresenterforwards the parameter to a managedMenuItem, which already handles the change.Commandbranch still overwrites an explicitIsEnabledvalue. That behaviour is unchanged here.Validation:
NativeMenuItemTestswith a command that accepts only anintparameter. The two tests that set and clearCommandParameterafterCommandfail without the fix and pass with it. The third test covers the item without a command.TrayIcon.Iconson anApplication,Commandbinding,x:Int32property element) with the runtime XAML loader. With theDataContextset before the load, the item is disabled without the fix and enabled with it. With theDataContextset after the load, the item is enabled in both cases. This check is not part of the PR.NativeMenuItemTestsandTrayIconTestsclasses inAvalonia.Controls.UnitTests. All pass.Checklist
Breaking changes
None. An item whose command result depends on the parameter can now change
IsEnabledwhen the parameter changes.Obsoletions / Deprecations
None.
Fixed issues
Fixes #21914