New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

chenyj796 opened this Issue Feb 1, 2019 · 5 comments


None yet
4 participants
Copy link

chenyj796 commented Feb 1, 2019

  • .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:


This comment has been minimized.

Copy link

brandonhood commented 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.


This comment has been minimized.

Copy link

brian-reichle commented Feb 7, 2019

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


This comment has been minimized.

Copy link

chenyj796 commented Feb 7, 2019

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


This comment has been minimized.

Copy link

brian-reichle commented 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:

var button = new Button();
button.Content = "Test";
button.Command = ...
button.CommandParameter = ...

This comment has been minimized.

Copy link

weltkante commented Feb 7, 2019

@brian-reichle but that would have to be done for every control to which you can attach a command, otherwise you have only fixed it for an individual case and all other controls still behave differently

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment