Custom attached events in WPF

依然范特西╮ 提交于 2019-12-06 03:32:18

You must either be not mapping the namespace, and/or attaching event like local:TagRectEvents.TagRectEnterEvent . You have to use TagRectEnter, and not TagRectEnterEvent.

Namespace mapping :

 xmlns:local="clr-namespace:WpfInfrastructure.WpfAttachedEvents"

Usage :

<Button Content="Press" local:TagRectEvents.TagRectEnter="MyHandler" Margin="25,43,36,161" />

Handler :

    public void MyHandler(object sender, RoutedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Hurray!");
    }

I used your code, it works correctly here.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!