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

  • Home
  • SEARCH
  • 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 8427805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:52:48+00:00 2026-06-10T04:52:48+00:00

I created a button whose commandparameter is set and command using a class that

  • 0

I created a button whose commandparameter is set and command using a class that implements ICommand interface. But my button is disabled. Why is that? I got this code from here: ICommand is like a chocolate cake

<Window x:Class="ICommand_Implementation_CSharp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ICommand_Implementation_CSharp"
    Title="MainWindow" Height="350" Width="525">
<Grid>      
    <Grid>
        <Grid.Resources>
            <local:HelloWorldCommand x:Key="hwc"  />
        </Grid.Resources>

        <Button Command="{StaticResource hwc}" CommandParameter="Hello" 
                Height="23" HorizontalAlignment="Left" Margin="212,138,0,0" 
                Name="Button1" VerticalAlignment="Top" Width="75">Button</Button>
    </Grid>
</Grid>

and my class is

class HelloWorldCommand:ICommand
{
    public bool CanExecute(object parameter)
    {
        return parameter != null;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        MessageBox.Show(parameter.ToString());
    }
}
  • 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-10T04:52:49+00:00Added an answer on June 10, 2026 at 4:52 am

    Well, this is very-very simple implementation of ICommand.

    As @JleruOHeP says, partially problem can be solved by swapping setters of Command and CommandParameter. But this is ugly way, because you have to remember the sequence every time.

    More correct way is to tell CommandManager to re-query states of command:

    public class HelloWorldCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            return parameter != null;
        }
    
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    
        public void Execute(object parameter)
        {
            MessageBox.Show(parameter.ToString());
        }
    }
    

    Now the sequence of setters is indifferent.
    To understand how CommandManager works, you can read this nice article from Josh Smith.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a button CSS class that uses background images with different positions
Actually I created a button using the cobogw widget library. But the button does
I've got a class whose content is set to a layout which has a
I created a button and added an action for it, but as soon as
I've created a button using HTML/CSS and a Java script. The button should direct
I've created a small user control consisting of a button whose content is an
I created a custom behavior that exposes some custom dependency properties whose values change
When I'm writing some JavaScript I have a set of interface buttons that have
I would like to create a button whose size is that of it's largest
I am new to Backbone.js. I have created a View for a button that

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.