lundi 9 mars 2015

Hosted Winform control does not respond to events from WPF

This is my first question in stackoverflow. Due to lack of reputations, I couldn't post any links or images. I've been working on the following issue for more than 2 days. Any help would greatly be appreciated.


Before I get into my question, here is what I have and what I'm expecting:


This is my Technology stack: MVVM - WPF - Prism.



  • I have a Windows Form which hosts WPF in an ElementHost control.

  • And then, I have a Winforms UserControl similar to DateTimePicker. This

    is hosted inside a WindowsFormsHost control.


The above scenario is un-avoidable for the following reasons:



  • The authorization dialog to all our applications is developed in Winforms, and takes a Winforms instance as its parameter. There is no WPF version introduced yet. Therefore, I had to use an ElementHost to host my View inside the Windows Form.

  • The Winforms control hosted inside my WPF is also un-avoidable. We have our own DateTime Winforms UserControl that behaves similar to the DateTimePicker Winforms control, but has lot more complexities involved. So, replacing this control with a WPF version is out of question.


Expected Functionality: I have a




  • WPF control (say, a textbox)




  • A DateTime Winforms UserControl that I was mentioning above.




  • And a Cancel button that basically resets the above controls.




When I hit the Cancel button, I'm publishing an event from the ViewModel, say RunViewModel to the WPF UserControl code behind file, say RunView.xaml.cs.


eventAggregator.GetEvent<ResetDateTimeEvent>().Publish(true);


In the code behind file, I've subscribed to the event as follows


eventAggregator.GetEvent<ResetDateTimeEvent>().Subscribe(ResetDateTimeHandler);


The WPF control resets to its default value, but the DateTime UserControl does not reset.


So, for testing purposes, I removed the ElementHost control, and just had my WPF View with a WindowsFormsHost control that hosts the DateTime Winforms UserControl, and a WPF "Cancel" button.


When I click on the button, the value on the DateTime control resets to its default value.


Then, I thought this might be an issue with my DateTime Winforms UserControl. So, I replaced my DateTime Winforms UserControl with a Winforms Textbox control in my actual application. So now the nesting is as follows:





WinForms-ElementHost-WPF-WindowsFormsHost-Winforms Textbox





Here is the xaml code.


<WindowsFormsHost x:Name="ReportFromDtTmHost" Margin="8,0" Grid.Column="0" LostFocus="ReportFromDtTmHost_LostFocus"> <WindowsFormsHost.Child> <winforms:TextBox x:Name="ReportFromDateTime"/> </WindowsFormsHost.Child> </WindowsFormsHost>


On Initial load, I’m loading the Textbox with “Initial Load Text” text


private void Window_Loaded(object sender, EventArgs e) { ReportFromDateTime.Text = "Initial Load Text"; }


As I was mentioning above, when I hit the Cancel button, this is what happens:



  • Publish the event from ViewModel


eventAggregator.GetEvent<ResetDateTimeEvent>().Publish(true);



  • Subscribe to the event in the code behind file (xaml.cs):


eventAggregator.GetEvent<ResetDateTimeEvent>().Subscribe(ResetDateTimeHandler);



  • EventHandler for the published event.


private void ResetDateTimeHandler(bool cancelClicked) { ReportFromDateTime.Text = "Reset to Default"; }


As you can see in the above code, I’m resetting the Text on clicking the Cancel button.


During Debugging, I could see the Text property being changed to "Reset to Default", but the UI does not show these changes.


Here is the wierd part:


The Child property on the WindowsFormsHost control is different from the actual “ReportFromDateTime” Textbox control.


While debugging, I could see that the Child and Name property on the WindowsFormsHost control were different. The Name property is empty,


ReportFromDtTmHost.Child.Name = ""


which rather should be ReportFromDateTime.


It almost seems like the Host and the Child controls are getting re-created.


As far as I see it, I think the extra level of nesting (WinForms-ElementHost-WPF-WindowsFormsHost-Winforms Textbox) might be causing issues during the interoperations between WPF and Winforms. I’ve done a lot of research and searched lot of links for suggestions. I found none pointing out this issue. Some of them were close. Here are a couple of links:


The below suggests to reproduce the message loop under the “Surrogate Windows Forma Message Loop” section.


(http://ift.tt/1KP2uLw)


Here is one more link that explains the issue with nesting under the Nesting section:


(http://ift.tt/1KP2wTB)


I apologize for being verbose. Just wanted you guys to get a clear picture of my problem. Please let me know if you have any questions regarding the post. Any suggestions would be greatly appreciated.


Aucun commentaire:

Enregistrer un commentaire