WPF 中的自定义附加事件
我可能在这里用错了术语,但我想我正在尝试创建一个附加事件。
在 Surface SDK 中,您可以执行以下操作:
<Grid Background="{StaticResource WindowBackground}" x:Name="Foo" s:SurfaceFrameworkElement.ContactChanged="Foo_ContactChanged"/>
我想创建一个自定义事件,可以以相同的方式在 XAML 中添加处理程序,但我遇到了麻烦。
我可以创建自定义路由事件,但 XAML 智能感知看不到它,并且如果我只是定期键入它,则不会添加事件处理程序。 这是我的事件定义:
public static class TagRectEvents
{
public static readonly RoutedEvent TagRectEnterEvent = EventManager.RegisterRoutedEvent(
"TagRectEnter", RoutingStrategy.Bubble, typeof( RoutedEventHandler ), typeof( TagRectEvents ) );
public static void AddTagRectEnterHandler( DependencyObject d, RoutedEventHandler handler )
{
UIElement element = d as UIElement;
if ( element == null )
{
return;
}
element.AddHandler( TagRectEvents.TagRectEnterEvent, handler );
}
public static void RemoveTagRectEnterHandler( DependencyObject d, RoutedEventHandler handler )
{
UIElement element = d as UIElement;
if ( element == null )
{
return;
}
element.RemoveHandler( TagRectEvents.TagRectEnterEvent, handler );
}
}
我是不是搞错了? 我看到的所有“附加行为”示例更多的是关于添加附加属性,然后使用设置该属性的元素执行操作。
I might be getting the terminology wrong here, but I think I'm trying to create an attached event.
In the Surface SDK, you can do things like:
<Grid Background="{StaticResource WindowBackground}" x:Name="Foo" s:SurfaceFrameworkElement.ContactChanged="Foo_ContactChanged"/>
I want to create a custom event for which a handler can be added in XAML in the same way, but I'm having trouble.
I can create a custom routed event, but the XAML intellisense doesn't see it and the event handler isn't added if I just type it in regularly. Here is my event definition:
public static class TagRectEvents
{
public static readonly RoutedEvent TagRectEnterEvent = EventManager.RegisterRoutedEvent(
"TagRectEnter", RoutingStrategy.Bubble, typeof( RoutedEventHandler ), typeof( TagRectEvents ) );
public static void AddTagRectEnterHandler( DependencyObject d, RoutedEventHandler handler )
{
UIElement element = d as UIElement;
if ( element == null )
{
return;
}
element.AddHandler( TagRectEvents.TagRectEnterEvent, handler );
}
public static void RemoveTagRectEnterHandler( DependencyObject d, RoutedEventHandler handler )
{
UIElement element = d as UIElement;
if ( element == null )
{
return;
}
element.RemoveHandler( TagRectEvents.TagRectEnterEvent, handler );
}
}
Am I just going about it all wrong? All of the "attached behavior" examples I see are more about adding an attached property, and then doing things with elements that set that property.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须不映射命名空间,和/或附加诸如
local:TagRectEvents.TagRectEnterEvent
之类的事件。 您必须使用TagRectEnter
,而不是TagRectEnterEvent
。命名空间映射:
用法:
处理程序:
我使用了你的代码,它在这里工作正常。
You must either be not mapping the namespace, and/or attaching event like
local:TagRectEvents.TagRectEnterEvent
. You have to useTagRectEnter
, and notTagRectEnterEvent
.Namespace mapping :
Usage :
Handler :
I used your code, it works correctly here.
为了使附加事件显示在 Intellisense 中,它必须位于驻留在卫星程序集(或 .dll 库)中的类中。 添加库的最简单方法是将“WPF 自定义控件库”项目添加到您的解决方案中。 使用 Wpf 控件库只是确保自动添加所有典型引用(使用 C# 类库则不会)。只要在 Themes/Generic 中删除其关联的 Style,就可以删除 CustomControl1.cs。 xaml。
In order to get the attached event to show up in Intellisense, it has to be in a class that resides in a satellite assembly -- or .dll library. The easiest way to add a library is add a "WPF Custom Control Library" project to your solution. Using a Wpf control library just ensures that all the typical References will be added automatically (which they won't with a C# class library.) You can delete the CustomControl1.cs just as long as you delete its associated Style in Themes/Generic.xaml.