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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:40:18+00:00 2026-05-16T17:40:18+00:00

How can I have a Command that can be called from both a Window

  • 0

How can I have a Command that can be called from both a Window and a UserControl (by hitting a button in either) that uses the same execute and can-execute methods? It seems like if I have the following in my UserControl’s XAML, it requires myCommandHandler and canExecuteMyCommand to be in my UserControl class:

<CommandBinding Command="{x:Static local:MyUserControl.MyCommand}"
                Executed="myCommandHandler"
                CanExecute="canExecuteMyCommand"/>

Then if I want to use the same Command in my Window, I again need myCommandHandler and canExecuteMyCommand defined in my Window’s class. How can I define a Command such that both my UserControl and Window can access it, but myCommandHandler and canExecuteMyCommand are only defined in one class? Do I need to create my own command class instead of declaring a static RoutedCommand field in MyUserControl?

  • 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-16T17:40:19+00:00Added an answer on May 16, 2026 at 5:40 pm

    In my opinion the best thing you can do is to write your own Command, because, as you said, you can reuse it in different Controls and Windows.

    The first approach to create your custom command would be to derive from ICommand. Here is an example of a basic command class:

    public abstract class BaseCommand : ICommand
    {
        protected IMyViewModel viewModel;
    
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    
        public BaseCommand(IMyViewModel viewModel)
        {
            this.viewModel = viewModel;
        }
    
        public abstract bool CanExecute(object parameter);
        public abstract void Execute(object parameter);
    }
    

    To use the command, for example on a button press the code would look something like this:

    <Button Command="{Binding Path=MyReuseableCommand,
                      UpdateSourceTrigger=PropertyChanged}" />
    

    I hope this will help you get the right direction.

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

Sidebar

Ask A Question

Stats

  • Questions 515k
  • Answers 515k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, it's the LIST mentioned - the => is just… May 16, 2026 at 6:30 pm
  • Editorial Team
    Editorial Team added an answer I think what you can hope for is going to… May 16, 2026 at 6:30 pm
  • Editorial Team
    Editorial Team added an answer This may help you: ANDROID ANIMATION SLIDEDOWN SLIDEUP Android Animation… May 16, 2026 at 6:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have an MSBuild task that executes (among other things) a call to xcopy.
I'm new to both Ruby and to Mac OSX, though I do have a
I have an application (the source for which I don't have), which can be
I have a scenario setup where I need to test to see if the
I am having trouble saving a dotplot to pdf when this command is done
I have a question about how to deploy WPF application into a PC without
I have an Access application where everytime a user enters the application, it makes
I have a program which makes use of a desktop Namespace extension. In Windows
On all my Windows servers, except for one machine, when I execute the following
We have a setup where most code, before being promoted to full production, is

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.