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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:43:04+00:00 2026-05-25T17:43:04+00:00

My routed events are hitting the root UI element before the child element. Is

  • 0

My routed events are hitting the root UI element before the child element. Is this expected? How can I have the routed events hit the child element first?

Objective: If text is typed anywhere other than “custom textbox”, put text in “default textbox”

Result: Window_PreviewTextInput is being hit before custom_PreviewTextInput, even if my cursor focus is on “Custom Textbox”

What should I do differently?


XAML

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" SizeToContent="WidthAndHeight"
        PreviewTextInput="Window_PreviewTextInput"
        >
    <Grid Margin="100,100,100,100">
        <StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="default" Width="100"/>
                <TextBox x:Name="defaultTB" Width="300" Height="50"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="custom" Width="100"/>
                <TextBox x:Name="custom" PreviewTextInput="custom_PreviewTextInput" Width="300" Height="50"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

Code Behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        //goal:  if text is typed anywhere except custom textbox, put text in default textbox
        private void Window_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Keyboard.Focus(defaultTB); 
        }

        //goal:  if text is typed in custom TB, put text there, and end the event routing
        private void custom_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = true; 
        }
    }
}
  • 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-25T17:43:04+00:00Added an answer on May 25, 2026 at 5:43 pm

    Routed Event could be a bubbling or tunneling. You’ve a tunneling event behaviour.

    From MSDN, UIElement.PreviewTextInput Event:

    Routing strategy – Tunneling

    The corresponding bubbling event is TextInput.

    Routed Events Overview – Routing Strategies:

    Bubbling: Event handlers on the event source are invoked. The routed
    event then routes to successive parent elements until reaching the
    element tree root. Most routed events use the bubbling routing
    strategy. Bubbling routed events are generally used to report input or
    state changes from distinct controls or other UI elements

    Direct: Only the source element itself is given the opportunity to
    invoke handlers in response. This is analogous to the "routing" that
    Windows Forms uses for events. However, unlike a standard CLR event,
    direct routed events support class handling (class handling is
    explained in an upcoming section) and can be used by EventSetter and
    EventTrigger.

    Tunneling: Initially, event handlers at the element tree root are
    invoked. The routed event then travels a route through successive
    child elements along the route, towards the node element that is the
    routed event source (the element that raised the routed event).
    Tunneling routed events are often used or handled as part of the
    compositing for a control, such that events from composite parts can
    be deliberately suppressed or replaced by events that are specific to
    the complete control. Input events provided in WPF often come
    implemented as a tunneling/bubbling pair. Tunneling events are also
    sometimes referred to as Preview events, because of a naming
    convention that is used for the pairs.

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

Sidebar

Related Questions

do routed events from wpf have something in common with the chain of responsibility
In WPF we have routed events. When should these be used instead of regular
I have a WPF form with 3 buttons and have routed events on them,
I have created a UserControl, and added a couple of custom routed events. The
I have a custom text box control which raises a routed event when its
How can I add the Click routed event to my custom class? This doesn't
I read this good article about Routed Events, and I understood why we need
Does routed events in WPF store strong reference to the handler that is attached
I am gathering all the routed events fired for the MouseRightDownButton and storing them
Does WPF support using a triggers that respond to routed events but only given

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.