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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:03:07+00:00 2026-05-26T07:03:07+00:00

In WPF, custom commands have event handlers such as command_Executed and command_CanExecute, the best

  • 0

In WPF, custom commands have event handlers such as command_Executed and command_CanExecute, the best practice is that the implementation logic should not be directly implemented in the code-behind class, and it should only contain function calls to the higher level implementation logic.

While making custom commands, if there is only one function call in the handler functions of the command, it is simple to place it directly in the code-behind, but what if there are multiple function calls, or function calls returning something on the basis of which a decision is to be made (specially in the case of command_CanExecute function that is disabling/enabling a command, has to make a decision when to enable or disable a command). The possibilities I have been thinking about are following, which one is most appropriate in terms of maintainablilty, keeping code clean, software engineering principles, and best practice?

  1. Keep all the function calls and decision making statements in the code-behind (in command_execute or some function in the code-behind that command_execute calls). It is fine if code is simple, but makes things ugly when there are many handlers in the code behind. Here there is no implementation code, only decision making code that i have mentioned earlier.

  2. Keep the implementation logic in the class that defines the command and makes it accessible through static properties. Now the handler in the code-behind will only have 1 function call and will receive the return value if it has to.

  3. Implement it in the higher level classes that implement the actual functionality of the application, so that the code-behind handler has only one function call. This makes the most sense, but this option creates doubts in my mind because in some cases, the command handler may have to call methods from many different unrelated classes, so where to place that logic is difficult to decide.

  4. Any other that I have missed.

  • 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-26T07:03:08+00:00Added an answer on May 26, 2026 at 7:03 am

    Usually I use a RelayCommand or DelegateCommand in the ViewModel (or in the Code-behind if you’re not using MVVM)

    private ICommand _customCommand;
    
    public ICommand CustomCommand
    {
        get
        {
            if (_customCommand == null)
            {
                _customCommand = new RelayCommand(
                    CustomCommandHandler, CanCustomCommandExecute);
            }
            return _customCommand;
        }
    }
    
    private void CustomCommandHandler()
    {
        // Execute Command
    }
    
    private bool CanCustomCommandExecute()
    {
        // Return true/false if the command can execute or not
    }
    

    XAML:

    <Button Command="{Binding CustomCommand}" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF application where I have defined custom commands that use the
I have a custom WPF control MyLine that should represent or not some text
In WPF, I have a custom control that inherits from TreeView. The code is
I have a WPF Keyboard Application, it is developed in such a way that
I have a WPF custom control that sometimes takes a while to render in
I have a WPF 4.0 application that utilizes some custom 16x16 icons in things
I have a custom WPF Line, that I whant to change the Style when
I am using wpf Custom Commands. While writing a custom Command, one need to
I am creating a custom WPF control that let's say for simplicity sake has
I'm trying to write a custom WPF ValidationRule to enforce that a certain property

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.