WPF prevent event bubbling outside of a control

PaN1C_Showt1Me picture PaN1C_Showt1Me · Apr 13, 2012 · Viewed 7.6k times · Source

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");
    }
}

Answer

Natxo picture Natxo · Apr 13, 2012

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.