Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8008529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:09:57+00:00 2026-06-04T18:09:57+00:00

I am trying to understand how to work with Commands. I read a lot

  • 0

I am trying to understand how to work with Commands.
I read a lot about commands, and I know, that most times commands are used within the MVVM pattern. Also I know, that there is a RoutedCommand class – which is often used to save time while developing.

But – I want to understand the basics – and exactly that’s the problem. Here we go:

In my app I defined a class ‘MyCommand’ :

 public class MyCommand :ICommand
{
    public void Execute(object parameter)
    {
        Console.WriteLine("Execute called!");
        CanExecuteChanged(null, null);
    }

    public bool CanExecute(object parameter)
    {
        Console.WriteLine("CanExecute called!");
        return true;
    }

    public event EventHandler CanExecuteChanged;
}

Well – to get a static access I decided to make a class, just for all application commands:

 public static class AppCommands
    {
        private static ICommand anyCommand = new MyCommand();

        public static ICommand AnyCommand
        {
            get { return anyCommand; }
        }
    }

Nearly there. Now I put two buttons in my MainWindow. One of them is ‘binded’ to the command:

<StackPanel>
    <Button Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" />
    <Button Content="Button" Height="23" Name="button2" Width="75" Command="{x:Static local:AppCommands.AnyCommand}" CommandParameter="Hello"/>
</StackPanel>

And here is the MainWindow.cs :

public MainWindow()


  {
        InitializeComponent();
        AppCommands.AnyCommand.CanExecuteChanged += MyEventHandler;
    }


    private void button1_Click(object sender, RoutedEventArgs e)
    {
       // Nothing for the moment
    }

    private void MyEventHandler(object sender, EventArgs e)
    {
        Console.WriteLine("MyEventHandler called");
    }

So – let’s run my project. As you can see, I made some console outputs.
If I click on button2, the output is:

CanExecute called!
Execute called!
CanExecute called!
MyEventHandler called

So, in my eyes this is what happens:
1.) the command on the button is ‘activated’. To check if the execute method should be called, the CanExecute method is called.
2.) If the CanExecute method returns true, then the Execute method is called.
3.) Within the execute method, I defined, that the event ‘CanExecuteChanged’ should be raised. Calling it will 1st check ‘CanExecute’ and then call the eventhandler.

This is not clear to me. The only way to call the event is within the Execute method. But the Execute method is called via Command logic after checking CanExecute. Calling the event also checks CanExecute, but why? I am confused.

And things are getting more confused, when I try to disable the button.
Lets say, there is a ‘CommandParameter’ – and ‘CanExecute’ now works with it. So it could be, that the method returns false. In this case the button is disabled – okay.

But: how do I reactivate it? As we already know: I can only raise the CanExecuteChange event from within my command class. So – as we are not able to click the disabled button – the command won’t call CanExecute (or even Execute) method.

Seems to me there is something important I can’t see – but I really can’t find it.

Could you please help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:10:00+00:00Added an answer on June 4, 2026 at 6:10 pm

    Your CanExecuteChanged shouldn’t be raised by Execute, it should be raised when CanExecute will start returning a different value. When that is depends on your command class. In the simplest form, you could add a property:

    public class MyCommand : ICommand
    {
        bool canExecute;
    
        public void Execute(object parameter)
        {
            Console.WriteLine("Execute called!");
        }
    
        public bool CanExecute(object parameter)
        {
            Console.WriteLine("CanExecute called!");
            return CanExecuteResult;
        }
    
        public event EventHandler CanExecuteChanged;
    
        public bool CanExecuteResult
        {
            get { return canExecute; }
            set {
                if (canExecute != value)
                {
                    canExecute = value;
                    var canExecuteChanged = CanExecuteChanged;
                    if (canExecuteChanged != null)
                        canExecuteChanged.Invoke(this, EventArgs.Empty);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand how can a magnetic link work, as I've read they
trying to understand how custom admin commands work, I have my project named mailing
I'm trying to understand how cursors work and I don't understand a portion of
Im trying to understand how class generics work and this bit just doesnt make
I am trying to understand why the script will work with #!/bin/bash but not
I'm trying to understand how parallelism might work using PLINQ, given delayed execution. Here
I'm trying to understand how ID3 tags work, so, after reading some documentation, I
I am trying to understand how Django + Jquery and Ajax calls work together.
I'm trying to understand how to get librsync to work (ie. make signature/make delta/patch)
I am trying to understand string interning and why is doesn't seem to work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.