WPF FrameworkElementFactory映像创建无法为MouseDownEvent添加AddHandler

gan*_*ran 4 c# wpf binding wpf-controls frameworkelementfactory

我正在使用Framework ElementFactory在DataTemplate中创建图像.在尝试处理Image类型的MouseDown事件时,会抛出异常 - "处理程序类型无效.

我们如何为Image类型的FrameworkElementFactory添加MouseDownEventHandler

FrameworkElementFactory imageSecondaryContent = new FrameworkElementFactory(typeof(Image));
imageSecondaryContent.SetValue(Image.WidthProperty, imgWidth);
imageSecondaryContent.SetValue(Image.VisibilityProperty, Visibility.Hidden);
imageSecondaryContent.Name = imageName;
Binding tmpBindingSecondaryContent = new Binding();
tmpBindingSecondaryContent.Source = IconLibary.GetUri(IconStore.ExclaminationPoint);
imageSecondaryContent.SetBinding(Image.SourceProperty, tmpBindingSecondaryContent);
imageSecondaryContent.AddHandler(Image.MouseDownEvent, new RoutedEventHandler(Test));
Run Code Online (Sandbox Code Playgroud)

最后一行抛出异常.请帮忙

gan*_*ran 9

我得到了答案.它是

imageSecondaryContent.AddHandler(Image.MouseDownEvent, new MouseButtonEventHandler(Test));
Run Code Online (Sandbox Code Playgroud)

如果您认为需要关闭,请关闭此问题.