Fix CanExecute(null) for value types without predicate#1207
Open
apachezy wants to merge 2 commits into
Open
Conversation
Author
|
@dotnet-policy-service agree [company="None"] |
Author
|
@dotnet-policy-service agree company="no company" |
apachezy
marked this pull request as draft
July 26, 2026 11:14
apachezy
marked this pull request as ready for review
July 26, 2026 11:21
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.
Closes #1206
When
RelayCommand<T>.CanExecute(object?)receivesnullfor a non-nullable value typeT, it unconditionally returnsfalse— even when nocanExecutepredicate was provided. This violates the documented contract: "The default return value for the CanExecute method istrue."This single-line fix distinguishes between the two cases:
true(the command is always executable, as documented)false(cannot meaningfully evaluatenullasT, safe fallback — unchanged from current behavior)In WPF applications on .NET Framework, the framework may call
CanExecute(null)during control initialization before theCommandParameterbinding has produced a value. With the unconditionalreturn false, commands without predicates are permanently disabled, as the initialfalseresult is cached and never re-evaluated.This fix is fully compatible with the reasoning in WindowsCommunityToolkit #3619: for commands with a predicate, mapping
nulltodefault(T)would conflate "no value" with a legitimate value (e.g.,0asSelectedIndex). The conclusion thatreturn falseis safe for that scenario is preserved here. The case of no predicate was not explicitly considered in that discussion — a command without a predicate should simply always be executable, as the class documentation states.PR Checklist
Other information
Change in
src/CommunityToolkit.Mvvm/Input/RelayCommand{T}.cs:if (parameter is null && default(T) is not null) { - return false; + return this.canExecute is null; }canExecuteRelayCommand<int>(Do)nullfalse❌true✅RelayCommand<int>(Do, i => i >= 0)falsefalse(unchanged)TryGetCommandArgument、ThrowArgumentExceptionForInvalidCommandArgument、Execute(object?)均未改动。