处理隧道自定义路由事件 [英] Handle tunneled custom routed event
问题描述
目前,我正在用C#WPF自定义路由事件我被困在一个问题进行试验。
这是我想做的事情:我想火从我的主窗口,它通过的StackPanel由Button类派生的自定义控制隧道自定义路由事件。然后自定义控制处理路由事件。
我的问题是,当我火的处理程序从未被称为事件
我的代码:
公共部分类主窗口:窗口
{
公共静态只读RoutedEvent MyRoutedEvent = EventManager.RegisterRoutedEvent (MyRoutedEvent,RoutingStrategy.Tunnel的typeof(RoutedEventHandler),typeof运算(的UIElement));
公共静态无效AddMyRoutedEventHandler(DependencyObject的D,RoutedEventHandler处理)
{
的UIElement UIE = D作为的UIElement;
如果(UIE!= NULL)
{
uie.AddHandler(MainWindow.MyRoutedEvent,处理程序);
}
}
公共静态无效RemoveMyRoutedEventHandler(DependencyObject的D,RoutedEventHandler处理)
{
的UIElement UIE = D作为的UIElement;
如果(UIE!= NULL)
{
uie.RemoveHandler(MainWindow.MyRoutedEvent,处理程序);
}
}
公共主窗口()
{
的InitializeComponent();
}
私人无效keyClassButton1_MyRoutedEvent(对象发件人,RoutedEventArgs E)
{
Console.Write(\\\
MyRoutedEvent!);
}
私人无效Window_MouseDown(对象发件人,MouseButtonEventArgs E)
{
RoutedEventArgs newEventArgs =新RoutedEventArgs(MyRoutedEvent,这一点);
的RaiseEvent(newEventArgs);
}
}
XAML代码:
<窗口x:类=RoutedEvent_Test.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml /演示
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:地方=CLR的命名空间:RoutedEvent_Test
标题=主窗口HEIGHT =350WIDTH =525的MouseDown =Window_MouseDown>
<网格和GT;
< StackPanel的名称=stackPanel1>
<局部:KeyClass X:NAME =keyClass1CONTENT =键类按钮HEIGHT =30地方:MainWindow.MyRoutedEvent =keyClassButton1_MyRoutedEvent>< /地方:KeyClass>
< / StackPanel的>
< /网格和GT;
< /窗GT;
好吧,我想通了由自己:
虽然我喜欢一千倍阅读它显然在MSDN说明指出:
隧道:首先,事件处理程序的元素树根是
调用。 路由事件,然后穿过连续
子沿线要素的路线,朝着那就是
路由事件源(的,所提出的路由元素节点元素事件)。
[...]
块引用>
我的一个隧道路由事件的第一个想法是:我火从主窗口事件它穿过的StackPanel到按钮元素。
而是:
你有自已的按钮启动它 - 那么它开始在根元素(主窗口),并经过控制层的按钮元件,它。解雇摆在首位的事件
我所做的就是:我从主窗口激活事件,因此不能去别的
$ b的任何地方$ bI'm currently experimenting with C# WPF custom routed events i got stuck at a problem. This is what i want to do: I want to fire a custom routed event from my main window which tunnels through a stackpanel to a custom control derived by the Button class. The custom control then handles the routed event.
My problem is when i fire the event the handler is never been called.
My code:
public partial class MainWindow : Window { public static readonly RoutedEvent MyRoutedEvent = EventManager.RegisterRoutedEvent("MyRoutedEvent", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(UIElement)); public static void AddMyRoutedEventHandler(DependencyObject d, RoutedEventHandler handler) { UIElement uie = d as UIElement; if (uie != null) { uie.AddHandler(MainWindow.MyRoutedEvent, handler); } } public static void RemoveMyRoutedEventHandler(DependencyObject d, RoutedEventHandler handler) { UIElement uie = d as UIElement; if (uie != null) { uie.RemoveHandler(MainWindow.MyRoutedEvent, handler); } } public MainWindow() { InitializeComponent(); } private void keyClassButton1_MyRoutedEvent(object sender, RoutedEventArgs e) { Console.Write("\nMyRoutedEvent!"); } private void Window_MouseDown(object sender, MouseButtonEventArgs e) { RoutedEventArgs newEventArgs = new RoutedEventArgs(MyRoutedEvent, this); RaiseEvent(newEventArgs); } }
XAML code:
<Window x:Class="RoutedEvent_Test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:RoutedEvent_Test" Title="MainWindow" Height="350" Width="525" MouseDown="Window_MouseDown"> <Grid> <StackPanel Name="stackPanel1"> <local:KeyClass x:Name="keyClass1" Content="key class button" Height="30" local:MainWindow.MyRoutedEvent="keyClassButton1_MyRoutedEvent"></local:KeyClass> </StackPanel> </Grid> </Window>
解决方案ok i figured it out by myself: Although i've read it like a thousand times it clearly states in the MSDN description:
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). [...]
My first idea of a tunneled routed event was: I fire a event from the main window and it goes through the stackpanel to the button element. BUT INSTEAD: You have to fire it from the button already - then it begins at the root element (main window) and goes through the control layers to the button element which fired the event in the first place.
What i did was: I fired the event from the main window so it couldn't go anywhere else
这篇关于处理隧道自定义路由事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!