WPF FrameworkElementFactory图像创建无法为MouseDownEvent添加处理程序

本文关键字:MouseDownEvent 添加 处理 程序 FrameworkElementFactory 图像 创建 WPF | 更新日期: 2023-09-27 18:08:03

我使用框架ElementFactory在DataTemplate中创建图像。当试图处理图像类型的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));

最后一行抛出异常。请帮助

WPF FrameworkElementFactory图像创建无法为MouseDownEvent添加处理程序

我得到了答案。这是

imageSecondaryContent.AddHandler(Image.MouseDownEvent, new MouseButtonEventHandler(Test));

如果你认为问题需要关闭,请关闭。