WinForms 中的 Interop WPF,如何处理来自 WPF 控件的事件

发布于 2024-10-17 04:02:05 字数 1268 浏览 3 评论 0原文

我正在 WinForms 应用程序中处理一些 WPF 互操作性。我有以下设置。

  • WinForms 用户控件 WFControl
    • WPF 用户控件 GalleryControl
      • 列表框图库项目
        • 列表框项目模板库项目

Winforms 托管GalleryControl,其中具有GalleryItems (ListBox),该GalleryItems (ListBox) 具有GalleryItem 的ItemTemplate。

现在,在 WFControl 中,我想查看 GalleryItems 何时触发其 SelectionChanged 事件。

我当前的尝试尝试:

  • 处理 GalleryControl 中的 SelectionChanged 事件,并让它引发一个我的 winforms 可以读取的单独的公共事件,但我无法像这样处理该事件,因为它不是路由事件。如果我能弄清楚如何处理这个问题,这就会起作用。适用代码:

    公共事件ClaimGallery SelectedClaimChanged;
    公共 ViewModels.InsuranceClaimViewModel ClaimViewModel { 获取;放; }
    公共 int SelectedClaimID
    {
        得到
        {
            return ((Models.InsuranceClaim) ClaimList.SelectedItem).ID;
        }
    }
    公共索赔库()
    {
        初始化组件();
        ClaimViewModel = new ViewModels.InsuranceClaimViewModel();
        DataContext = ClaimViewModel;
        ClaimList.ItemsSource = ClaimViewModel.InsuranceClaims;
        ClaimList.SelectionChanged += ClaimSelectionChanged;
    }
    
    私有无效ClaimSelectionChanged(对象发送者,EventArgs e)
    {
        //这是不起作用的部分
        ClaimList.RaiseEvent(new RoutedEventArgs(SelectedClaimChanged, this));
    } 
    

我还看到,我可能可以通过浏览订阅 WFControl 中实际事件的某些控件树找到 ListBox,但我似乎不知道如何在互操作控件中执行此操作。

I'm working with some WPF Interoperability in a WinForms application. I have the following set up.

  • WinForms UserControl WFControl
    • WPF UserControl GalleryControl
      • ListBox GalleryItems
        • ListBox ItemTemplate GalleryItem

Winforms hosting the GalleryControl, which has GalleryItems (ListBox) that has a ItemTemplate of GalleryItem.

Now in the WFControl I want to see when GalleryItems has it's SelectionChanged Event triggered.

My current attempts have tried to:

  • Handle the SelectionChanged Event in GalleryControl and have it raise a seperate public event that my winforms can read, but I can't handle the event like that since it's not a routed event. This would work if I could figure out how to handle that. applicable code:

    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));
    } 
    

I've also seen that I could potentially find the ListBox via some control tree browsing the subscribe to the actual event in the WFControl but I can't seem to figure how to do this in an interop'd control.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

庆幸我还是我 2024-10-24 04:02:05

我当前的项目中也遇到类似的问题,我正在按照您描述的方式解决它。
WPF 控件重新引发公共(正常)事件,然后由 WinForms 控件处理。

老实说,我不明白您所说的必须进行路由才能由 Winforms 处理的部分。

我的 winforms 可以读取,但我无法处理这样的事件,因为它不是路由事件

您可以使用“+=”来处理这个事件......

I have similar problems in my current project, and I'm solving it the way you describe.
The WPF controls re-raises a public (normal) event, that is then handled by the WinForms control.

Honestly I don't get the part where you are stating that is has to be routed in order to be handled by Winforms.

my winforms can read, but I can't handle the event like that since it's not a routed event

you use "+=" to handle this one ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文