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 8877941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:35:32+00:00 2026-06-14T19:35:32+00:00

Although I have found several answers to this question I somehow don’t get it.

  • 0

Although I have found several answers to this question I somehow don’t get it. So please excuse me asking.

I have a WPF application following the MVVM pattern. It contains a button which is bound to a command in the view model:

<button Content="Login" Command="{Binding ProjectLoginCommand}"/>

The commands are using RelayCommand. Now I would like to do the following:

  • The user clicks the button and the corresponding command is executed. This works.
  • Within this command another button shall be deactivated, i.e. it shall not be clickable.

I found that this should be possible using CanExecute but being honest: I simply don’t get it. Hoe can I set the button to enabled/disabled?

This is the RelayCommand.cs:

namespace MyApp.Helpers {
    class RelayCommand : ICommand {

    readonly Action<object> execute;
    readonly Predicate<object> canExecute;

    public RelayCommand(Action<object> execute) : this(execute, null) {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
      if (execute == null)
          throw new ArgumentNullException("execute");

      this.execute = execute;
      this.canExecute = canExecute;           
    }

    public bool CanExecute(object parameter)
    {
      return canExecute == null ? true : canExecute(parameter);
    }

  public event EventHandler CanExecuteChanged
  {
      add { CommandManager.RequerySuggested += value; }
      remove { CommandManager.RequerySuggested -= value; }
  }

  public void Execute(object parameter)
  {
      execute(parameter);
  }
 }
}

This is how I call the command:

RelayCommand getProjectListCommand;

public ICommand GetProjectListCommand {
    get {
        if (getProjectListCommand == null) {
            getProjectListCommand = new RelayCommand(param => this.ProjectLogin());
        }
        return getProjectListCommand;
    }
}
  • 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-14T19:35:33+00:00Added an answer on June 14, 2026 at 7:35 pm

    When you use the RelayCommand you can specify two methods. The first method is the main method you want to run when the command is invoked. The second method you use to add in checks such as validation, and this should return a bool. if it returns false, then the main method will not run.

    How it affects the button you have the command bound to, is it will run the boolean method continually, and while it return false, then the button the command is bound to will be disabled.

    So in your command property:

    public ICommand GetProjectListCommand {
    get {
        if (getProjectListCommand == null) {
            getProjectListCommand = new RelayCommand(param => this.ProjectLogin(), CanProjectLogin());
        }
        return getProjectListCommand;
    }
    

    Add in new method:

    public bool CanProjectLogin()
    {
        //here check some properties to make sure everything is set that you'd want to use in your ProjectLogin() method
    }
    

    you can see how the CanExecute works if you put a break point in the bool method.

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

Sidebar

Related Questions

Although I have found partial and indirect answers to this question (see, e.g., this
Although I have found some solutions to this problem, none of them refer to
Hopefully a simple question although one I have found impossible to answer myself using
I see several others have posted this question, however, none of the solutions I've
I have searched the site and although I have found questions and answers similar
Although I have research this topic I don't really seem to understand how to
Although I have my php validation doing this, thought I may as well pop
I'm kind of new to WPF although I have some experience in Forms, and
I hope this is a relatively easy problem although I have spent hours websearching
While looking into the IndexedDB API, I have found several examples of event handlers

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.