Skip to content

CommandParameter invalidates CanExecute - #4217

Merged
singhashish-wpf merged 1 commit into
dotnet:mainfrom
miloush:CommandParameter
Jul 21, 2022
Merged

singhashish-wpf merged 1 commit into
dotnet:mainfrom
miloush:CommandParameter

Conversation

@miloush

@miloush miloush commented Feb 24, 2021 •

Copy link
Copy Markdown
Contributor

Fixes Issue #3452 #4078 (also discussed in #316)

In summary, the issue is that

<MenuItem CommandParameter="P" Command="C" />

and

<MenuItem Command="C" CommandParameter="P" />

behave differently. In the latter case, C.CanExecute is called to determine whether the element should be enabled, but since the parameter has not been parsed yet, CanExecute is called with a null argument. After the parameter is parsed, the status of the command is not re-evaluated and therefore the element stays disabled when it shouldn't be.

A common workaround is to use the former syntax.

Description

Adds property changed callback to CommandParameterProperty wherever applicable (MenuItem, ButtonBase, Hyperlink, RibbonGallery, RibbonTextBox) to call UpdateCanExecute.

This way, changing command parameter makes it re-evaluate command's CanExecute value. Most notably, this will make the order of Command and CommandParameter properties in XAML irrelevant.

Note that ThumbButtonInfo already implements this behaviour.

Customer Impact

Not taking this fix retains the existing XAML attributes order significance (and the inconsistency with ThumbButtonInfo behaivour).

Testing

This has not been tested as the branch does not build at the time of submitting this PR. Happy to test if further instructions provided.

Risk

No public API changes, 5 private static methods and callback instances in DP properties introduced. All changes in the PresentationFramework assembly.

The new behaviour will cause the CanExecute method of commands to be called more often (with every parameter change), and specifically twice during loading of the element (which could be avoided by deferring CanExecute to EndInit phase).

If user changes both parameter and command and changes the command parameter first, with the new behaviour the existing command will potentially be called with a command parameter of an unexpected type. Since the API accepts object though, the implementations of CanExecute should be able to handle both null and any object type.

@miloush
miloush requested a review from a team as a code owner February 24, 2021 18:56
@ghost ghost added the PR metadata: Label to tag PRs, to facilitate with triage label Feb 24, 2021
@ghost
ghost requested review from SamBent, fabiant3 and ryalanms February 24, 2021 18:56
Base automatically changed from master to main March 17, 2021 17:38
@ryalanms ryalanms added the Investigate Requires further investigation by the WPF team. label May 6, 2021
@ryalanms

ryalanms commented May 6, 2021

Copy link
Copy Markdown
Member

@miloush: As soon as Sam has bandwidth, he will review this. Thanks.

@miloush
miloush force-pushed the CommandParameter branch from ee7069f to d78af6f Compare April 3, 2022 16:13
@lindexi

lindexi commented Apr 8, 2022

Copy link
Copy Markdown
Member

A year has passed..

@dipeshmsft dipeshmsft self-assigned this May 7, 2022
@ghost ghost assigned miloush May 7, 2022
@pchaurasia14 pchaurasia14 added the Community Contribution A label for all community Contributions label Jul 20, 2022
@singhashish-wpf
singhashish-wpf merged commit 7dd94fe into dotnet:main Jul 21, 2022
@dipeshmsft dipeshmsft removed Queue for test pass Investigate Requires further investigation by the WPF team. Under Review labels Jul 21, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Sep 9, 2022
@miloush
miloush deleted the CommandParameter branch November 1, 2022 01:57
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants