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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:22:16+00:00 2026-06-01T20:22:16+00:00

Let’s begin with the element tree. The topmost element is a WPF window which

  • 0

Let’s begin with the element tree. The topmost element is a WPF window which has registered ApplicationCommands.Find command.
Some child elements have KeyBinding with the gesture key ENTER pointing at this command. That is OK, if someone presses ENTER the command will be executed.
Among them I have a Custom control with a popup for searching and selecting some elements. At this place though,
I don’t want to allow the bubbling of the key event outside of the custom control. But I set e.handled = true for
KeyDown += new KeyEventHandler (..) , which is supposed to be a bubbling routed event, all controls (Textboxes, etc) inside of that custom control will ignore any kind of input.
But I just want to stop bubbling at the level of the CustomControl, speaking: from the leaf until the topmost control parent, not further !
Why is that? I am not filtering PreviewKeyDown, so the event must be propagating until the leaf, and then it should go back to the parent, but it does not.

Thank you

EDITED: Sample code:

<UserControl x:Class="WpfApplication5.TestUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
    <StackPanel Orientation="Vertical">        
        <TextBox />
        <TextBox />
        <TextBox />
    </StackPanel>
</UserControl>


/* 
 * I want to stop bubbling at this level. Let us say if we click 'enter' on the level of the TextBox leaf, 
 * it should propagate until the TestUserControl is reached and then stop.
 */
public partial class TestUserControl : UserControl
{
    public TestUserControl()
    {
        InitializeComponent();
    }
}


<Window x:Class="WpfApplication5.Window6"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication5"
    Title="Window6" Height="300" Width="300">
    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Find" Executed="CommandBinding_Executed" />
    </Window.CommandBindings>
    <Window.InputBindings>
        <KeyBinding Gesture="Enter" Command="ApplicationCommands.Find" />
    </Window.InputBindings>
    <Grid>
        <local:TestUserControl />
    </Grid>
</Window>

public partial class Window6 : Window
{
    public Window6()
    {
        InitializeComponent();
    }

    private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        MessageBox.Show("Executed");
    }
}
  • 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-06-01T20:22:18+00:00Added an answer on June 1, 2026 at 8:22 pm

    You want to bridge the event from your Custom Control to your Window, right?
    Then you must relaunch the event from the Window for the system to take care of it. If you only mark it as handled it means that no further actions will be made.

    The code should be something like this:

    private void CustomControl_KeyDown(object sender, KeyEventArgs e)
    {
        e.Handled = true;
        var relaunchedEvent = new KeyEventArgs(e.KeyboardDevice, e.InputSource, e.Timestamp, e.Key);
        relaunchedEvent.RoutedEvent = Keyboard.KeyDownEvent;
        relaunchedEvent.Source = e.OriginalSource;
        TopMostWindow.RaiseEvent(relaunchedEvent);
    } 
    

    — EDIT —

    Test the code above with the following xaml, to see how the keydown event is fired in the window without executing the command.

    <Window x:Class="WpfApplication5.Window6"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication5"
        Title="Window6" Height="300" Width="300"
        x:Name="TopMostWindow">
        <Grid>
            <Grid KeyDown="CustomControl_KeyDown">
                <local:UserControl1/>
            </Grid>
            <Grid.CommandBindings>
                <CommandBinding Command="ApplicationCommands.Find" Executed="CommandBinding_Executed" />
            </Grid.CommandBindings>
            <Grid.InputBindings>
                <KeyBinding Key="Enter" Command="ApplicationCommands.Find" />
            </Grid.InputBindings>
        </Grid>
    </Window>
    

    Important notes:

    • Commands are at a higher level, so it doesn’t matter wether you use bubbling or tunneling events, eventually the command will be executed unless the event is marked as handled.
    • Raising a Keydown event doesn’t actually mean to press the key, as text composition will not take place.

    My advice for your case is to filter the keydown event in your user control, mark it as handled if e.Key=Key.Enter, as Enter doesn´t do much on textboxes. Otherwise you’ll have to simulate key press.

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

Sidebar

Related Questions

let us suppose some (legacy) code, which cannot be touched, declare struct B{ public:
Let's consider the following scenario: a function which can generate code colors from white
Let's say I have window.open (without name parameter), scattered in my project and I
Let's say there is a graph and some set of functions like: create-node ::
Let's say, if I need to assign an id to element with first name,
Let's say I'm making a tool to help epicureans keep track of which delicacies
Let's assume that a user votes for some movies in a scale of 1
Let's say I have this string which I want to put in a multidimensional
Let's say I have the string: hello world; some random text; foo; How could
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>

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.