Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return custom values from WPF dialog

Tags:

c#

wpf

.net-4.0

I am using Window.ShowDialog() method to fetch some values from the user. However, the dialog will only return a nullable bool.

How can I get my WPF window to return a Tuple<string,string> or any other type?

like image 618
Nobody Avatar asked Sep 21 '10 15:09

Nobody


2 Answers

You could add a new method to your Window, something like this:

public Tuple<string, string> ShowTupleDialog()
{
    var retTuple = new  Tuple<string, string>();
    this.ShowDialog();
    // values from dialog to retTuple (maybe use 
    //databinding and return an already defined tuple)
    return retTuple;
}
like image 137
Nate Avatar answered Nov 06 '22 07:11

Nate


You don't need to return that value from the ShowDialog method, as explained here: "A Nullable<(Of <(T>)>) value of type Boolean that signifies how a window was closed by the user".

You can simply store the value you want to return in a property or field and get it:

window.ShowDialog();
Tuple<string, string> value = window.InputValue;
like image 20
Damian Schenkelman Avatar answered Nov 06 '22 05:11

Damian Schenkelman