简体   繁体   English

如何为事件处理程序创建事件处理程序?

[英]How to create a event handler for event handler?

I'm creating a Custom Control inherit TextBox , however, as 我正在创建一个Custom Control继承TextBox ,因为 PreviewTextInput and PreviewTextInput TextInput is not handled in TextBox by default, I wanted implement a TextInput没有在处理TextBox默认情况下,我想实现 PreviewTextInputLinked and PreviewTextInputLinked TextInputLinked for my control, just for developer can add or remove these two events using += and -= TextInputLinked用于我的控件,仅适用于开发人员,可以使用+=-=添加或删除这两个事件

I have created a TextCompositionEventHandler called 我创建了一个TextCompositionEventHandler名为 PreviewTextInputLinked TextInputLinked

public event TextCompositionEventHandler /*PreviewTextInputLinked*/ TextInputLinked = delegate { };
public MyTextBox()
{
   this.AddHandler(TextBox.PreviewTextInputEvent, /*PreviewTextInputLinked */TextInputLinked, true);
}

In the program XAML 在程序XAML中

/*<UControls:MyTextBox VerticalAlignment="Top" AcceptLetters="9" Mask=""
                     Width="122" 
                     PreviewTextInputLinked="MyTextBox_PreviewTextInputLinked"/>*/
<UControls:MyTextBox VerticalAlignment="Top" Width="122" 
                     TextInputLinked="MyTextBox_TextInputLinked"/>

Code behind 后面的代码

private void /*MyTextBox_PreviewTextInputLinked*/ MyTextBox_TextInputLinked(object sender,
                                              TextCompositionEventArgs e)
{
   MessageBox.Show("test");
}

But it is not doing anything, maybe I'm doing wrong, but I don't know what more can I do 但是它什么也没做,也许我做错了,但是我不知道该怎么办

Where is the problem? 问题出在哪儿?

PreviewTextInput will work since this is a tunnelling event whereas TextInput is a bubble event . PreviewTextInput将起作用,因为这是一个tunnelling eventTextInput是一个bubble event In case it is handled while routing up, it won't bubble up to visual tree that's why hooking to PreviewTextInput works and TextInput doesn't because it might be handled somewhere en-route of bubbling. 如果在路由过程中对其进行处理,则不会冒泡到可视树,这就是钩挂到PreviewTextInput起作用而TextInput不能起作用的原因,因为它可能在冒泡过程中的某个地方处理。

AddHandler is a way to achieve that which you are using. AddHandler是一种实现所使用的方法。 Notice its third bool parameter which corresponds to handledEventsToo ie you still want handler to be called even it is handled below down Visual Tree by some element. 注意它的第三个bool参数,它对应于handledEventsToo也就是说,即使它在Visual Tree下方被某个元素处理,您仍然希望调用处理程序。

Now issue in your code is you pass an empty delegate to it which will get execute just like you want but you manually need to call other delegates (hooked via XAML to that event) which can be achieved like this - 现在,您的代码中出现的问题是, you pass an empty delegate给它,该you pass an empty delegate按照您的意愿执行,但是您需要手动调用其他委托(通过XAML将该事件挂钩),可以这样实现:

public event TextCompositionEventHandler TextInputLinked = (sender, args) =>
{
   MyTextBox textBox = (MyTextBox)sender;
   if (textBox.TextInputLinked != null)
   {
       foreach (var handler in textBox.TextInputLinked.GetInvocationList())
       {
          if (handler.Target != null) <-- Check to avoid Stack overflow exception
          {
             handler.DynamicInvoke(sender, args);
          }
       }
   }
};

public MyTextBox()
{
    this.AddHandler(TextBox.TextInputEvent, TextInputLinked, true);
}

Now other handlers will be called as you desired. 现在将根据需要调用其他处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM