how to close window form which is hosting WPF user control from within a WPF user control

I want to close a window form that is hosting a WPF user control. Something like this as used while closing a current form in window application. But for WPF application I am not able to get reference to user controls parent

How to get Form which is hosting this control so that I can close my form

this.Close()

Answers


Add to your WpfControl property

public Form FormsWindow { get; set; }

In your WinForm add event handler for ElementHost's event ChildChanged:

using System.Windows.Forms.Integration; 

public MyForm() {
    InitializeComponent();
    elementHost.ChildChanged += ElementHost_ChildChanged;
}
void ElementHost_ChildChanged(object sender, ChildChangedEventArgs e) {
    var ctr = (elementHost.Child as UserControl1);
    if (ctr != null)
        ctr.FormsWindow = this;
}

After that you can use the FormsWindow property of your WpfControl to manipulate window. Example:

this.FormsWindow.Close();

Need Your Help

Accessing Values From Object property that is an array

php arrays object properties

How can I access values from an object property that is an array?

Font select drop down list in angularjs

angularjs fonts google-webfonts

I want to create a form that can change the font of the text given,I coded the drop down box but it is hard to get the value of it and the preview of the font.I want to change the font of the "Text...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.