路由事件问题 - 在子元素之前击中根 UI 元素
我的路由事件在子元素之前先到达根 UI 元素。这是预期的吗?如何让路由事件首先命中子元素?
目标:如果在“自定义文本框”以外的任何地方键入文本,请将文本放入“默认文本框”
结果:即使我的光标焦点位于“自定义文本框”上,Window_PreviewTextInput 也会在 custom_PreviewTextInput 之前被点击
我应该采取哪些不同的做法?
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>
代码隐藏:
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;
}
}
}
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
路由事件可以是冒泡或隧道。你有隧道事件行为。
来自 MSDN,UIElement.PreviewTextInput 事件:
路由事件概述 - 路由策略:
Routed Event could be a bubbling or tunneling. You've a tunneling event behaviour.
From MSDN, UIElement.PreviewTextInput Event:
Routed Events Overview - Routing Strategies: