title: How to get an instance of hosting WinForm from a WPF user control?
body: <p>I have a WPF <code>UserControl</code> which is being hosted either in a WPF window or in a Windows Forms <code>Form</code>. When the user presses the "X" button to close the hosting window/form, I want to grab this closing event and do some operations. </p>
  
  <p>For this purpose I have subscribed to the loading event of the <code>UserControl</code> to grab the hosting windows/form instance and subscribe to its closing event.</p>
  
  <p>It is working fine with an WPF window, but when I try to do the same with a <code>Form</code>, I get an error and I am unable to proceed.</p>
  
  <p><strong>WPFUsercontrol.xaml.cs</strong></p>
  
  <pre><code>private void WpfUsercontrol_OnLoaded(object sender, RoutedEventArgs e)
  {
      Window window = Window.GetWindow(this);
      if (window != null)
          window.Closing += window_closing;
  
      Form form = this.Parent as Form;    
      //Error: Cannot convert from System.Windows.DependencyObject to System.Windows.Forms.Form    
  }
  </code></pre>
  
  <p>How can I achieve the same functionality of closing a <code>Form</code> as I am doing with a WPF Window?</p>