Skip to content

CommandParameter is null when call CanExecute method at first time #316

Closed

Description

@chenyj796
  • .NET Core Version: (3.0.0-preview-27122-01)
  • Windows version: (windows 10)
  • Does the bug reproduce also in WPF for .NET Framework 4.6.1?: Yes

Problem description:

I have a TestCommand as below:

public class TestCommand : ICommand
{
    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
    }
}

and then I use it in a xaml file:

<Button Content="Test" Command="{StaticResource testCommand}" CommandParameter="abcdef"/>

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):

<Button Content="Test" CommandParameter="abcdef" Command="{StaticResource testCommand}"/>

Expected behavior:
The parameter's value is not null at each time, regardless of the XAML order

Minimal repro:

Activity

  1. bhood-zorus commented on Feb 4, 2019

    @bhood-zorus

    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.

  2. brian-reichle commented on Feb 7, 2019

    @brian-reichle

    @brandonhood, or the button could just defer the call between BeginInit and EndInit?

  3. chenyj796 commented on Feb 7, 2019

    @chenyj796
    Author

    In my opinion, the XAML equals to the codes:

    var button = new Button
    {
        Content = "Test",
        Command = ...
        CommandParameter = ...
    };

    and the CanExecute method will be called by some methods such as Load ...

  4. brian-reichle commented on Feb 7, 2019

    @brian-reichle

    The .NET framework version of the xaml parser honours the ISupportInitialize interface (which is implemented by FrameworkElement), so its actually more like this:

    var button = new Button();
    ((ISupportInitialize)button).BeginInit();
    button.Content = "Test";
    button.Command = ...
    button.CommandParameter = ...
    ((ISupportInitialize)button).EndInit();
  5. weltkante commented on Feb 7, 2019

    @weltkante
  6. added
    Enhancement RequestedProduct code improvement that does NOT require public API changes/additions
    on Apr 4, 2019
  7. added this to the Future milestone on Apr 4, 2019
  8. added
    Good First Issuemetadata: Issue should be easy to implement, good for first-time contributors
    on Apr 4, 2019
  9. stevenbrix commented on Apr 4, 2019

    @stevenbrix
    Contributor

    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.

  10. vflame commented on May 13, 2019

    @vflame

    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

  11. MichaelVach commented on Jul 5, 2019

    @MichaelVach

    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.

  12. MichaelVach commented on Jul 8, 2019

    @MichaelVach

    The attached test project can be used to verify the proposed solution.

    CommandCanExecuteProject.zip

  13. removed
    Good First Issuemetadata: Issue should be easy to implement, good for first-time contributors
    on Jul 16, 2019
  14. 9 remaining items

  15. GF-Huang commented on Nov 18, 2021

    @GF-Huang

    Microsoft has shifted its focus to WinUI 3, give it up, rubbish WPF.

  16. sungaila commented on Nov 22, 2021

    @sungaila

    Microsoft has shifted its focus to WinUI 3, give it up, rubbish WPF.

    Abandoned UI frameworks I am aware of since the introduction of WPF:

    • Silverlight
    • WinRT
    • WinPRT
    • Windows Universal Apps (for Windows 8)
    • UWP (WinUI 2)

    If you believe that WinUI 3 will stay alive for more than 2-3 years, then you haven't been paying attention.

  17. GF-Huang commented on Nov 22, 2021

    @GF-Huang

    If WinUI 3 focus on Win32 App, that will be difference.

  18. heku commented on Sep 9, 2022

    @heku

    Got same issue today, .NET 6 WPF.

  19. GF-Huang commented on Sep 9, 2022

    @GF-Huang

    Got same issue today, .NET 6 WPF.

    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.

  20. pchaurasia14 commented on Sep 9, 2022

    @pchaurasia14
    Contributor

    @heku - This has been fixed in RC1 release. You may try it out.

  21. pchaurasia14 commented on Sep 9, 2022

    @pchaurasia14
    Contributor

    @DaveInCaz - We haven't yet. But we'll be publishing them soon.

  22. DavidHollman commented on Sep 9, 2022

    @DavidHollman

    @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.

  23. pchaurasia14 commented on Sep 9, 2022

    @pchaurasia14
    Contributor

    @DaveInCaz - Yup, that's the one I think it is.

  24. DavidHollman commented on Sep 9, 2022

    @DavidHollman

    @pchaurasia14 what does it mean that .NET 6.0 RC1 was released in 2021, but this commit was done only in July 2022?

  25. pchaurasia14 commented on Sep 10, 2022

    @pchaurasia14
    Contributor

    @DaveInCaz - I meant, .NET 7 RC1.

  26. singhashish-wpf commented on Jan 6, 2023

    @singhashish-wpf
    Contributor

    Closing as the PR is already merged for this.

  27. ghost removed this from the Future milestone on Jan 6, 2023
  28. ghost locked as resolved and limited conversation to collaborators on Feb 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design DiscussionOngoing discussion about design without consensusEnhancement RequestedProduct code improvement that does NOT require public API changes/additions

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions