wpf return object from window in csharp

To return an object from a WPF window in C#, you can create a public property in your window class and assign the value to it before closing the window. Here's an example:

main.cs
public partial class MyWindow : Window
{
    public MyObject MyObject { get; set; }

    public MyWindow()
    {
        InitializeComponent();
    }

    private void CloseButton_Click(object sender, RoutedEventArgs e)
    {
        MyObject = new MyObject() // create your object here
        this.Close();
    }
}
317 chars
16 lines

In your main window or viewmodel where you open the window, you can wait for the window to close and then access the object through the property.

main.cs
MyWindow myWindow = new MyWindow();
myWindow.ShowDialog();
// after the window is closed
MyObject myObject = myWindow.MyObject;
128 chars
5 lines

related categories

gistlibby LogSnag