开发者

Routed Events problem - hitting the root UI element before the child element

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.Window开发者_Python百科s.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; 
        }
    }
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜