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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:21:37+00:00 2026-05-12T10:21:37+00:00

I have a static class which contains a RoutedUICommand that I would like to

  • 0

I have a static class which contains a RoutedUICommand that I would like to use in binding.

public static class CommandLibrary
{
    public static ProjectViewModel Project { get; set; }

    public static RoutedUICommand AddPage { get; private set; }

    static CommandLibrary()
    {
        AddPage = new RoutedUICommand("AddPage", "AddPage", typeof(CommandLibrary));

    }

    public static void AddPage_Executed(object sender, ExecutedRoutedEventArgs args)
    {
        Project.AddPage();
    }

    public static void AddPage_CanExecute(object sender, CanExecuteRoutedEventArgs args)
    {
        // We need a project before we can add pages.
        if (Project != null)
        {
            args.CanExecute = true;
        }
        else
        {
            // Did not find project, turning Add Page off.
            args.CanExecute = false;
        }
    }
}

When I attempt to create a CommandBinding for this AddPage command, VS throws a tantrum, complaining that it can’t find AddPage_CanExecute in Window1… Which makes no sense considering that all the examples I’ve seen indicate this XAML should be fine considering the code I have in place:

<Window x:Class="MyProject.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyProject">
    <Menu>
        <Menu.CommandBindings>
            <CommandBinding Command="local:CommandLibrary.AddPage" 
                            Executed="AddPage_Executed" CanExecute="AddPage_CanExecute" />
        </Menu.CommandBindings>
        <MenuItem Header="_Page">
            <MenuItem Header="_New" Command="local:CommandLibrary.AddPage" />
        </MenuItem>
    </Menu>
</Window>

I’ve also tried not including the Menu.CommandBindings section and simply using this (as per this question which suggests this but is not specific):

<MenuItem Header="_New" Command="{x:Static local:CommandLibrary.AddPage}" />

That staunched the flow of errors but the menu item it generates is always disabled! CanExecute never seems to get called. I’m assuming the binding is failing in this case, as well, though more quietly.

Why does VS hate my command and refuse to look in the right place to find the Executed and CanExecute methods? I’ve seen a number of examples (in Pro WPF by Matthew McDonald and on several custom command tutorials online) that have done this as I am doing it.

  • 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-12T10:21:37+00:00Added an answer on May 12, 2026 at 10:21 am

    A CommandBinding is just like any other element in your visual tree. Any events specified on it will be handled by the root of your visual tree (your Window in this case). That means if you move the AddPage_Executed and AddPage_CanExecute to your Window’s code behind, it will work. This allows you to use the same command in many UI components but have different handlers.

    I see, however, that your command executes some logic against your view model. To save you some time and frustration, understand that routed commands are the wrong solution here. Instead, encapsulate your command in your view model something like this:

    public class ProjectViewModel
    {
        private readonly ICollection<PageViewModel> _pages;
        private readonly ICommand _addPageCommand;
    
        public ProjectViewModel()
        {
            _pages = new ObservableCollection<PageViewModel>();
            _addPageCommand = new DelegateCommand(AddPage);
        }
    
        public ICommand AddPageCommand
        {
            get { return _addPageCommand; }
        }
    
        private void AddPage(object state)
        {
            _pages.Add(new PageViewModel());
        }
    }
    

    A DelegateCommand is an implementation of ICommand that invokes delegates to execute and query the command. That means the command logic is all wrapped up in the command and you don’t need a CommandBinding to provide handlers (you don’t need a CommandBinding at all). So your view just binds to your VM as follows:

    <MenuItem Header="_New" Command="{Binding AddPageCommand}"/>
    

    I suggest you read through this series of posts to give you more context:

    • View Models: POCOs versus DependencyObjects
    • ViewModel
    • DelegateCommand
    • ActiveAwareCommand
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which contains a static field that acts like a singleton
I have a class which contains a static collection that can be used across
I have a DLL which contains a class with static members . I use
In my testing project, I have a static class called FixtureSetup which I use
I have a non-static class called ImplementHeaderButtons which contains a non-static public method called
I have inherited code which contains static nested classes as: public class Foo {
I have a static class in a shared project, which I want to extend
I have this static class which contains a static variable (a simple int). I've
I have a class which contains a static member, a map of strings to
I have a C++ class which contains only static data members. I noticed the

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.