Home > Mobile >  How do close the User control wpf when opened from a Windows.forms
How do close the User control wpf when opened from a Windows.forms

Time:10-21

I have called a WPF form from Windows.Forms programatically. Now the concern here is i need to close the wpf form when clicked the button inside the wpf form but its not happening and staying in the WPF screen only. Any idea how do we close the wpf form and proceed for further steps.

   Form dialog = new Form();
                    dialog.Width = 200;
                    dialog.Height = 100;

ctrlHost = new ElementHost();
                    ctrlHost.Dock = DockStyle.Fill;
                    prompt.Controls.Add(ctrlHost); 
                    wpfControl = new User_Control();
                    wpfControl.InitializeComponent();
                    ctrlHost.Child = wpfControl;

                    wpfControl.dialogResult = dialog.ShowDialog();

If you see my above code i have created the form in the run time and assigned the wpf to that. But after showdialog i am struck in that screen and not able to come out. So need a code to close the wpf and come out of that form.

Any idea how do we come out of the wpf back. As i have created some buttons in WPF and need to come out when clicked on that button to the next code where i have loaded the form.

CodePudding user response:

Well a solution could be manage a custom Form and an event.

I've created a simple test project with:

  1. FormMain: the main form with a button to open a HostForm
  2. FormHost: a form with the ElementHost where the wpf usercontrol will be showed
  3. TestControl: the wpf control to show on the ElementHost

Code of the FormMain

 public partial class FormMain : Form
 {
        public FormMain()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var dialog = new FormHost();
            dialog.Width = 200;
            dialog.Height = 100;

            var result = dialog.ShowDialog();

        }   
 }

Has you can see i just called the FormHost and wait it's result

Code of the FormHost:

 public partial class FormHost : Form
{
    public FormHost()
    {
        InitializeComponent();

        elementHost1.Dock = DockStyle.Fill;
        var wpfControl = new TestControl();
        wpfControl.InitializeComponent();

        //Code your events management here 
        wpfControl.Close  = WpfControl_Close; 
        
        elementHost1.Child = wpfControl;

    }

    private void WpfControl_Close(DialogResult result)
    {
        //Manage the result as you like 
        DialogResult = result;
        Close();
    }    
}

In the FormHost I've managed the initilization of the WPF control also the management of events.

Xaml code of the TestControl

<UserControl x:Class="TestHost.TestControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:TestHost"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
    <Button Content="Ok" Width="60" VerticalAlignment="Center"  HorizontalAlignment="Center" Click="Ok_Click" Margin="10"/>
    <Button Content="Cancel" Width="60" VerticalAlignment="Center"  HorizontalAlignment="Center"   Margin="10" Click="Cancel_Click"/>
</StackPanel>

Code behind of the TestControl:

 public partial class TestControl : System.Windows.Controls.UserControl
{
    public event Action<DialogResult> Close;

    public TestControl()
    {
        InitializeComponent();
    }

    private void Ok_Click(object sender, RoutedEventArgs e)
    {
        Close?.Invoke(DialogResult.OK);
    }

    private void Cancel_Click(object sender, RoutedEventArgs e)
    {
        Close?.Invoke(DialogResult.Cancel);
    }
}

As you can see i've created an event and managed on the parent Form of the ElementHost, than I've closed the FormHost and setted its DialogResult.

  • Related