Question

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.

Was it helpful?

Solution

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 ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top