You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
CommandParameter is null when call CanExecute method at first time #316
Actual behavior:
At first time the CanExecute method is called, the parameter's value is null。
If I change the XAML property's order as below, the parameter's value is abcdef (that's ok):
I'm having uncomfortable flashbacks to when I first learned of this ☹️
I'm not sure anything can be done about this. XAML properties are assigned sequentially, and I doubt that's negotiable. This would probably require some kind of hack in the XAML reader.
The .NET framework version of the xaml parser honours the ISupportInitialize interface (which is implemented by FrameworkElement), so its actually more like this:
I agree that this behavior is incorrect. I think something like this should be pretty straightforward to do. FrameworkElement has an OnInitialized override that Button should be able to invoke the command from. However, we don't know who is currently depending on the current behavior, so this is something that we can look into fixing after the initial release of .NET Core 3.
This issue been around for a while. The easiest fix for now is to change the order so that CommandParameter appears before Command but order should not matter.
we see that the error occurs in the
PresentationFramework.dll!MS.Internal.Commands.CommandHelpers.CanExecuteCommandSource(System.Windows.Input.ICommandSource commandSource)
method
The idea is to delay the execution of this method until the WPF elements are initialized. See the above linked commit 0c6b967 for the details.
Stop expecting any functional updates, improvements, and fixes in WPF. The vast majority of updates are xaml-related, which is likely just incidental to Microsoft updating other UI frameworks that use xaml.
Activity
bhood-zorus commentedon Feb 4, 2019
I'm having uncomfortable flashbacks to when I first learned of this☹️
I'm not sure anything can be done about this. XAML properties are assigned sequentially, and I doubt that's negotiable. This would probably require some kind of hack in the XAML reader.
brian-reichle commentedon Feb 7, 2019
@brandonhood, or the button could just defer the call between
BeginInitandEndInit?chenyj796 commentedon Feb 7, 2019
In my opinion, the XAML equals to the codes:
and the CanExecute method will be called by some methods such as Load ...
brian-reichle commentedon Feb 7, 2019
The .NET framework version of the xaml parser honours the ISupportInitialize interface (which is implemented by FrameworkElement), so its actually more like this:
weltkante commentedon Feb 7, 2019
stevenbrix commentedon Apr 4, 2019
I agree that this behavior is incorrect. I think something like this should be pretty straightforward to do.
FrameworkElementhas anOnInitializedoverride thatButtonshould be able to invoke the command from. However, we don't know who is currently depending on the current behavior, so this is something that we can look into fixing after the initial release of .NET Core 3.vflame commentedon May 13, 2019
This issue been around for a while. The easiest fix for now is to change the order so that CommandParameter appears before Command but order should not matter.
Related: https://stackoverflow.com/questions/335849/wpf-commandparameter-is-null-first-time-canexecute-is-called
MichaelVach commentedon Jul 5, 2019
I would like to propose a solution for the Button and MenuItem cases mentioned in the linked Stack Overflow question above.
Looking at stack trace that leads to the bug for the Button case
StackTrace leading to ICommand.CanExecute Bug.txt
we see that the error occurs in the
PresentationFramework.dll!MS.Internal.Commands.CommandHelpers.CanExecuteCommandSource(System.Windows.Input.ICommandSource commandSource)
method
The idea is to delay the execution of this method until the WPF elements are initialized. See the above linked commit 0c6b967 for the details.
MichaelVach commentedon Jul 8, 2019
The attached test project can be used to verify the proposed solution.
CommandCanExecuteProject.zip
9 remaining items
GF-Huang commentedon Nov 18, 2021
Microsoft has shifted its focus to WinUI 3, give it up, rubbish WPF.
sungaila commentedon Nov 22, 2021
Abandoned UI frameworks I am aware of since the introduction of WPF:
If you believe that WinUI 3 will stay alive for more than 2-3 years, then you haven't been paying attention.
GF-Huang commentedon Nov 22, 2021
If WinUI 3 focus on Win32 App, that will be difference.
heku commentedon Sep 9, 2022
Got same issue today, .NET 6 WPF.
GF-Huang commentedon Sep 9, 2022
Stop expecting any functional updates, improvements, and fixes in WPF. The vast majority of updates are xaml-related, which is likely just incidental to Microsoft updating other UI frameworks that use xaml.
pchaurasia14 commentedon Sep 9, 2022
@heku - This has been fixed in RC1 release. You may try it out.
pchaurasia14 commentedon Sep 9, 2022
@DaveInCaz - We haven't yet. But we'll be publishing them soon.
DavidHollman commentedon Sep 9, 2022
@pchaurasia14 it looks like this was the change you're referring to, is that right?
#4217
I could see that in the list of commits for the RC1 release.
pchaurasia14 commentedon Sep 9, 2022
@DaveInCaz - Yup, that's the one I think it is.
DavidHollman commentedon Sep 9, 2022
@pchaurasia14 what does it mean that .NET 6.0 RC1 was released in 2021, but this commit was done only in July 2022?
pchaurasia14 commentedon Sep 10, 2022
@DaveInCaz - I meant, .NET 7 RC1.
singhashish-wpf commentedon Jan 6, 2023
Closing as the PR is already merged for this.