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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:31:21+00:00 2026-05-19T23:31:21+00:00

I am trying to access commands that are defined in MainWindow.xaml in another window.

  • 0

I am trying to access commands that are defined in MainWindow.xaml in another window. I am only able to get grayed out titles of these commands. I am wondering what should be should be done in order to get a full access.

Sample of the command:

public partial class MainWindow : Window
{
    public static RoutedUICommand AddCommand1 = new RoutedUICommand("Command ", "command1", typeof(MainWindow));

    public MainWindow()
    {
        InitializeComponent();
        this.CommandBindings.Add(new CommandBinding(AddCommand1, AddCommand1Executed));
    }

    private void AddCommand1Executed(object sender, ExecutedRoutedEventArgs e)
    {
        AddNewItem picker = new AddNewItem();
        picker.ShowDialog();
    }

I access these command in style through databinding:

<Menu x:Name="TaskMenuContainer"><MenuItem x:Name="menuItem" Header="TASKS" ItemsSource="{Binding}" Template="{DynamicResource TaskMenuTopLevelHeaderTemplateKey}">
<MenuItem.ItemContainerStyle>
    <Style TargetType="{x:Type MenuItem}">
        <Setter Property="Command" Value="{Binding}" />
        <Setter Property="Header" Value="{Binding Text, RelativeSource={RelativeSource Self}}" />
        <Setter Property="CommandTarget" Value="{Binding RelativeSource={RelativeSource Self}}"/>
    </Style>
</MenuItem.ItemContainerStyle>

These commands work in pages that is loaded inside MainWindow.xaml through frame. However, if I have pop up window that is not part of MainWindow.xaml these commands are only grayed out and not functional anymore (cannot be executed). Any advice is highly appreciated!

  • 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-19T23:31:21+00:00Added an answer on May 19, 2026 at 11:31 pm

    The way you define the command, you define it for a particular window. If you want to handle the command globally, at the application level, you can use CommandManager.RegisterClassCommandBinding:

    First, define you command in a separate static class:

    public static class GlobalCommands
    {
        public static RoutedUICommand AddCommand1 = new RoutedUICommand("Command ", "command1", typeof(MainWindow));
    }
    

    Then, in you window or whatever place you want to put the command logic, register the command handlers:

    public partial class MainWindow : Window
    {
        static MainWindow()
        {
           CommandManager.RegisterClassCommandBinding(typeof(Window), new CommandBinding(GlobalCommands.AddCommand1, AddCommand1Executed, CanAddExecute));
        }
    
        private static void AddCommand1Executed(object sender, ExecutedRoutedEventArgs e)
        {
            AddNewItem picker = new AddNewItem();
            picker.ShowDialog();
        }
    }
    

    And in your menu style you should change the binding to x:Static:

    <Setter Property="Command" Value="{x:Static my:GlobalCommands.AddCommand1}" />
    

    When the command is routed, when checking for command bindings in each active element in the UI, the bindings registered for each element’s class will also be checked. By registering the binding here, you can cause every instance of a class to be able to handle the specific command.

    So, in the above example, the type Window is used and this will cause the routing to find the command binding in any instance of Window, once the routing reaches that instance in its search for a command binding.

    You could instead, for example, restrict the handling to a specific subclass of Window, so that the command will only be bound in an instance of that subclass. Or you can use some other UI element type, so that that the presence of that specific type of element will cause the event to be handled. Just set the owning type for the registered command binding appropriately for your specific needs.

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

Sidebar

Related Questions

I'm trying to automate access to a website that get reports from a Microsoft-IIS/6.0
Im trying to access the Magento customer session in another part of my website.
I'm trying to access a member structs variables, but I can't seem to get
I am currently trying to figure out how to connect to another server via
I'm trying to set up another thread that currently checks the status of the
Trying to access a Servlet from a button on HTML page //Html Page FORM
Im trying to access a Sql CE 2005 database on a windows mobile device
I'm trying to access datasource before I bind it to grid. How do I
I'm trying to access list data using SSRS 2008. I have created an XML
I am trying to access a control inside a Repeater. The control is inside

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.