cantdoanything33 cantdoanything33 - 1 year ago 65
C# Question

Passing value from wpf form to winform

In my main Winform program I have a button with which I open my WPF dll in new window. I load my value through a consructor to WPF window. The problem is getting it back.

I initialize WPF window with this code on buttonClick on winform:

var wpfwindow = new MyWpfProgram.MainWindow(MyString);
ElementHost.EnableModelessKeyboardInterop(wpfwindow);
wpfwindow.Show();


In my WPF program I have Save button which should somehow pass the value back. How can i achieve this?

Answer Source

In MainWindow you can create an event

public event EventHandler<SaveEventArgs> Save;

with SaveEventArgs being like

public class SaveEventArgs : EventArgs
{
    public MySaveData Whatever { get; set; }
}

now - when you create the WPF window - you can subscribe to this event and handle it appropriately

var wpfwindow = new MyWpfProgram.MainWindow(MyString);
wpfwindow.Save += WpfWindow_OnSave;
ElementHost.EnableModelessKeyboardInterop(wpfwindow);
wpfwindow.Show();

[...]

private void WpfWindow_ONSave(object sender, SaveEventArgs eventArgs)
{
    // do whatever you need
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download