简体   繁体   English

如何在XAML中为附加事件添加多个处理程序?

[英]How to add multiple handlers to an attached event in XAML?

Code Sample: 代码示例:

<DataTemplate x:Key="NodeDataTemplate">
    <Border Style="{StaticResource nodeBorderStyle}"
                        MouseEnter="SetMouseCursor_Arrow"
                        MouseLeave="SetMouseCursor_ScrollAll"
                        MouseLeftButtonDown="ViewLink"
                        MouseLeftButtonDown="SetFlagForCursorTracking">
....
</DataTemplate>

I want to add 2 handlers to a particular event like shown above. 我想为上面显示的特定事件添加2个处理程序。 However this won't compile - 'attribute can be set more than once'. 但是这不会编译 - '属性可以设置多次'。 I have multiple methods because they do different things (are named appropriately). 我有多种方法,因为它们做了不同的事情(被恰当地命名)。 eg the first handler has nothing in common with the second. 例如,第一个处理程序与第二个没有任何共同点。

My other option was to kludge a SetFlagForCursorTrackingAndCheckForViewLink method - which is "Yech!". 我的另一个选择是使用SetFlagForCursorTrackingAndCheckForViewLink方法 - 这是“Yech!”。 Any ideas ? 有任何想法吗 ?

Please try to just add one handler which will subsequently call some event handling method. 请尝试添加一个处理程序,随后将调用一些事件处理方法。 Or add just one handler which will subsequently call your desired two event handling methods - this will be more elegant. 或者只添加一个处理程序,随后调用所需的两个事件处理方法 - 这将更加优雅。

You can do that in code behind, 你可以在后面的代码中做到这一点,

UIObject.AddHandler( AttachedEventContainer.AttachedEvent, your_handler);

UIObject.AddHandler( AttachedEventContainer.AttachedEvent, your_handler);

I have not tried or tested this, if this gives any error then you have no choice but use a method and call two event handlers manually in that method and attached method as event handler. 我没有尝试或测试过这个,如果这给出了任何错误,那么你别无选择,只能使用一个方法并在该方法中手动调用两个事件处理程序,并将附加方法作为事件处理程序。

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

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