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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:12:39+00:00 2026-05-15T05:12:39+00:00

I am using a delegate command . I have noticed that regardless CanExecute is

  • 0

I am using a delegate command .
I have noticed that regardless CanExecute is true or false execute is always called.
Is this correct?
I would have assumed that Execute would have been called only if CanExecute is true.

Could you clarify?

Thanks a lot

EDITED test shows that Save is always called

             [TestFixture]
                public class Can_test_a_method_has_been_called_via_relay_command
                {
                    [Test]
                    public void Should_be_able_to_test_that_insert_method_has_been_called_on_repository()
                    {
                        var mock = new Mock<IEmployeeRepository>();
                        var employeeVm = new EmployeeVM(mock.Object) {Age = 19};
                        employeeVm.SaveCommand.Execute(null);
                        mock.Verify(e=>e.Insert(It.IsAny<Employee>()));
                    }
                    [Test]
                    public void Should_be_able_to_test_that_insert_method_has_not_been_called_on_repository()
                    {
                        var mock = new Mock<IEmployeeRepository>();
                        var employeeVm = new EmployeeVM(mock.Object) { Age = 15 };
                        employeeVm.SaveCommand.Execute(null);
                        mock.Verify(e => e.Insert(It.IsAny<Employee>()),Times.Never());
                    }
                }

                public class EmployeeVM:ViewModelBase
                {
                    private readonly IEmployeeRepository _employeeRepository;

                    public EmployeeVM(IEmployeeRepository employeeRepository)
                    {
                        _employeeRepository = employeeRepository;
                    }


                    private bool _hasInserted;
                    public bool HasInserted
                    {
                        get { return _hasInserted; }
                        set
                        {
                            _hasInserted = value;
                            OnPropertyChanged("HasInserted");
                        }
                    }

                    private int _age;
                    public int Age
                    {
                        get { return _age; }
                        set
                        {
                            _age = value;
                            OnPropertyChanged("Age");
                        }
                    }
                    private string _name;
                    public string Name
                    {
                        get { return _name; }
                        set
                        {
                            _name = value;
                            OnPropertyChanged("Name");
                        }
                    }
                    private RelayCommand _saveCommand;
                    public ICommand SaveCommand
                    {
                        get
                        {
                            return _saveCommand ?? (_saveCommand = new RelayCommand(x => Save(), x => CanSave));
                        }
                    }
                    private  bool CanSave
                    {
                        get 
                        {
                            return Age > 18;
                        }
                    }

                    private void Save()
                    {
                        Insert();
                        HasInserted = true;
                    }

                    private void Insert()
                    {
                        _employeeRepository.Insert(new Employee{Age = Age,Name = Name});
                    }
                }

                public interface IEmployeeRepository
                {
                    void Insert(Employee employee);
                }

                public class Employee
                {
                    public string Name { get; set; }
                    public int Age { get; set; }
                }
            }
  • 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-05-15T05:12:40+00:00Added an answer on May 15, 2026 at 5:12 am

    Your test methods are not testing what WPF will be doing run-time.

    WPF will first determine if CanExecute evaluates to true – if it is not, the Button/MenuItem/InputBinding etc. is disabled and thus cannot be fired.

    As I mentioned in my comment – this is only enforced by convention.

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

Sidebar

Related Questions

This code shows that the Delegate Command from the Visual Studio MVVM template works
I have a textblock with command binding and using Prism library. this is the
I'm having a problem with using delegate inside Blocks. With ARC on, this is
This questions involves 2 different implementations of essentially the same code. First, using delegate
I am using vb.net 2010 and I have created a program that uses sockets
I'm using the System.Diagnostics.Process class to execute a command line program. I am using
I have using a DelegateCommand and i want to pass the UserControl with Command.
This is a follow-up to: Projection using async delegate/lambda Apparently, the Async CTP has
I have created an MSI package (Using InstallShield - IS) that installs my application
Using a delegate I can call any function asynchronously. From the documentation I understand

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.