Zherquen
asked on
How to Close Form with Wpf User Control in Element Host
I have a windows form that has Wpf Content inside. I would like to put a close button in wpf user control to close windows form. Is there any easy way to do this?
Thanks in advance.
Thanks in advance.
Howdy
An alternative to the above suggestion would be to create an interface eg iWindowsFormsHostedActions that contains definitions of any number of actions you may want to perform from the wpf control on you winforms host ie your close member.
Your winforms host then implements this interface. Your wpf controls have a property added to them that is of type iWindowsFormHostedAction (good idea here to be consistent with implementing another interface or create yourself an abstract class for consistency and interoperability). So after you create the instance of your wpf control in the winform form you also set the new property = this
At this point you can then simply call the close method on the iWindowsFormHostedAction property.
This method involves just a touch more work upfront but once you have set it up you can add additional functionality very quickly and easily.
Hope that helps
James
An alternative to the above suggestion would be to create an interface eg iWindowsFormsHostedActions
Your winforms host then implements this interface. Your wpf controls have a property added to them that is of type iWindowsFormHostedAction (good idea here to be consistent with implementing another interface or create yourself an abstract class for consistency and interoperability). So after you create the instance of your wpf control in the winform form you also set the new property = this
At this point you can then simply call the close method on the iWindowsFormHostedAction property.
This method involves just a touch more work upfront but once you have set it up you can add additional functionality very quickly and easily.
Hope that helps
James
ASKER
Thanks for detailed description James. I see what you mean but not totally. Can you show me that with an example please?
Howdy
Sorry its taken a little while to get back to you, attached you should find some rough pseudo code providing an example of the above description. Im happy to provide any more help you may need.
James
Sorry its taken a little while to get back to you, attached you should find some rough pseudo code providing an example of the above description. Im happy to provide any more help you may need.
James
// this is our shared interface for performing actions on our windows host
public interface iWindowsFormsHostedActions
{
void CloseWindow();
}
// interface to impliment on wpf control
public interface iWpfInteraction
{
iWindowsFormsHostedActions WinFormsHost {get;set;}
}
//winforms form
public class MainWindow: Form, iWindowsFormsHostedActions
{
public MainWindow()
{
// I assume the following is similiar to how you are adding your wpf control
System.Windows.Forms.Integration.ElementHost host = new System.Windows.Forms.Integration.ElementHost();
System.Windows.UIElement obj = new WpfControl();
((iWpfInteraction)obj).WinFormsHost = this;
host.Child = obj;
host.Dock = DockStyle.Fill;
this.Controls.Add(host);
}
public void CloseWindow()
{
this.Close();
}
}
public class WpfControl : UserControl, iWpfInteraction,iWindowsFormsHostedActions
{
WindowsFormsHostedActions WinFormsHost
{
get
{
// get logic
}
set
{
// set logic
}
}
public void CloseWindow()
{
if (WinFormsHost != null)
{
WinFormsHost.CloseWindow();
}
}
}
ASKER
Thanks for your reply James. While building your sample it get an error like this.
Missing partial modifier on declaration of type 'WpfOnForms.WpfControl'; another partial declaration of this type exists
I generate the public class WpfControl inside WpfControl.xaml.cs (Changed WindowsFormsHostedActions --> iWindowsFormsHostedActions )
Where might the error raised from?
Thanks In advance
Missing partial modifier on declaration of type 'WpfOnForms.WpfControl'; another partial declaration of this type exists
I generate the public class WpfControl inside WpfControl.xaml.cs (Changed WindowsFormsHostedActions --> iWindowsFormsHostedActions
Where might the error raised from?
Thanks In advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Good Evening James,
I have download the example and complied. It is working as intended :). This example is going to improve my programming quality by the way. Thanks for your advices and helps :) .
I have download the example and complied. It is working as intended :). This example is going to improve my programming quality by the way. Thanks for your advices and helps :) .
Close the windows form on trigger.