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

  • Home
  • SEARCH
  • 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 3849266
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:49:41+00:00 2026-05-19T16:49:41+00:00

My problem is that I would like to handle a commands in multiple places.

  • 0

My problem is that I would like to handle a commands in multiple places. For instance I have my custom UserControl where a Button is bound to some command. I have a command binding in that control but I also have a command binding in a Window which uses this control.

My aim is to perform some action inside the control while not interrupting handling of the command in the Window.

I tried experimenting with Executed and PreviewExecuted events but with no luck. Then I simulated the problem in a single window (code posted below).

<Window x:Class="CommandingEvents.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:CommandingEvents="clr-namespace:CommandingEvents" 
    Title="Window1" Height="300" Width="300">
<Window.CommandBindings>
    <CommandBinding 
        Command="{x:Static CommandingEvents:Window1.Connect}"
        Executed="CommandBindingWindow_Executed"
        PreviewExecuted="CommandBindingWindow_PreviewExecuted"/>
</Window.CommandBindings>
<Grid>
    <Grid.CommandBindings>
        <CommandBinding 
        Command="{x:Static CommandingEvents:Window1.Connect}"
        Executed="CommandBindingGrid_Executed"
        PreviewExecuted="CommandBindingGrid_PreviewExecuted" />
    </Grid.CommandBindings>
    <Button Command="{x:Static CommandingEvents:Window1.Connect}" 
            CommandTarget="{Binding RelativeSource={RelativeSource Self}}"
            Content="Test" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>

namespace CommandingEvents
{
    public partial class Window1
    {
        public static readonly RoutedUICommand Connect = new
            RoutedUICommand("Connect", "Connect", typeof(Window1));

        public Window1()
        {
            InitializeComponent();
        }

        private void CommandBindingWindow_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Console.WriteLine("CommandBindingWindow_Executed");
            e.Handled = false;
        }

        private void CommandBindingGrid_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Console.WriteLine("CommandBindingGrid_Executed");
            e.Handled = false;
        }

        private void CommandBindingWindow_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            Console.WriteLine("CommandBindingWindow_PreviewExecuted");
            e.Handled = false;
        }

        private void CommandBindingGrid_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            Console.WriteLine("CommandBindingGrid_PreviewExecuted");
            e.Handled = false;
        }
    }
}

When I hit the button only “CommandBindingWindow_PreviewExecuted” is printed out. Why is that? I tried to set e.Handled to false but it doesn’t make a difference. Can anyone explain this behaviour?

  • 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-19T16:49:41+00:00Added an answer on May 19, 2026 at 4:49 pm

    I have no idea why that happens (and how it isn’t a bug) but here is what was written in the WPF wiki:

    There is a particularity about
    CommandBinding which is extremely
    interesting and important to know.

    The CommandManager uses routed events
    to notify the different CommandBinding
    objects that a command execution was
    invoked (through default gestures,
    input bindings, explicitly, etc.).

    Up to now, this is fairly
    straightforward. However, what is more
    important is that CommandBinding will
    mark the routed event from the
    CommandManager as handled as soon as a
    handler gets executed (either
    PreviewExecuted or Executed).

    Finally, even if your handler has a
    prototype that matches a delegate
    called ExecutedRoutedEventHandler, the
    Executed event from the CommandBinding
    is not a RoutedEvent but a normal CLR
    event. Setting or leaving the
    e.Handled flag to false will change
    nothing.

    Therefore, as soon as an Executed or
    PreviewExecuted handler is invoked,
    the RoutedCommand will halt its
    routing.

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

Sidebar

Related Questions

I have a problem that I would like have solved via a SQL query.
I am wondering how you would approach this problem I have two Taxrates that
I have a problem that would be easily solved if I could somehow inflate
I thought that it would be fun to present some classic CS problems and
One problem that I come across regularly and yet don't have a solution to
I have a problem that confuses my users, being that although an item is
I have the problem that an specific step in Ant can only be executed
I am working on a Surface application and I have a problem with multiple
I have a main application that loads up some plugins. These plugins are loaded
I am using Ruby on Rails 3 and I would like to handle user

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.