C# WinForms中的互操作WPF,如何处理WPF控件中的事件
我正在WinForms应用程序中使用一些WPF互操作性。我有以下设置C# WinForms中的互操作WPF,如何处理WPF控件中的事件,c#,wpf,winforms-interop,C#,Wpf,Winforms Interop,我正在WinForms应用程序中使用一些WPF互操作性。我有以下设置 WinForms用户控件WFControl WPF用户控件GalleryControl 列表框GalleryItems 列表框项目模板GalleryItem 承载GalleryControl的Winforms,其中GalleryItems(ListBox)的ItemTemplate为GalleryItem 现在在WFControl中,我想查看GalleryItems何时触发了它的SelectionChanged
- WinForms用户控件WFControl
- WPF用户控件GalleryControl
- 列表框GalleryItems
- 列表框项目模板GalleryItem
- 列表框GalleryItems
- WPF用户控件GalleryControl
SelectionChanged
事件
我目前的努力是:
- 在GalleryControl中处理SelectionChanged事件,并让它引发一个单独的公共事件,我的winforms可以读取该事件,但我不能这样处理该事件,因为它不是路由事件。如果我能弄明白怎么处理的话,这就行了。适用代码:
public event ClaimGallery SelectedClaimChanged; public ViewModels.InsuranceClaimViewModel ClaimViewModel { get; set; } public int SelectedClaimID { get { return ((Models.InsuranceClaim) ClaimList.SelectedItem).ID; } } public ClaimGallery() { InitializeComponent(); ClaimViewModel = new ViewModels.InsuranceClaimViewModel(); DataContext = ClaimViewModel; ClaimList.ItemsSource = ClaimViewModel.InsuranceClaims; ClaimList.SelectionChanged += ClaimSelectionChanged; } private void ClaimSelectionChanged(object sender, EventArgs e) { //This is the part that doesn't work ClaimList.RaiseEvent(new RoutedEventArgs(SelectedClaimChanged, this)); }
我还看到,我可以通过一些控件树来找到列表框,浏览WFControl中的subscribe to the actual event(订阅实际事件),但我似乎不知道如何在互操作控件中实现这一点。我在当前项目中遇到了类似的问题,我正在按照您描述的方式解决它。 WPF控件重新引发一个公共(正常)事件,然后由WinForms控件处理 老实说,我不明白您所说的为了由Winforms处理而必须路由的部分 我的winforms可以读取,但我无法处理这样的事件,因为它不是路由事件
您使用“+=”来处理这个问题…我在当前项目中遇到了类似的问题,我正在按照您描述的方式解决它。 WPF控件重新引发一个公共(正常)事件,然后由WinForms控件处理 老实说,我不明白您所说的为了由Winforms处理而必须路由的部分 我的winforms可以读取,但我无法处理这样的事件,因为它不是路由事件
您可以使用“+=”来处理此项…我无法访问WinForm中的GalleryItems.SelectionChanged我无法访问winforms中的GalleryItems.SelectionChanged