I’m trying to get value after window dialog is closed:
public partial class MyDialogWindow: Window
{
public string selectedItem = "";
public MyDialogWindow(string selectedItem)
{
InitializeComponent();
this.selectedItem = selectedItem;
}
...
}
// To call dialog
string result = "";
MyDialogWindow dialog = new MyDialogWindow(result);
if (form.ShowDialog().Value)
{
string res = result;
}
But ‘result’ always is empty. In winforms I can get this result, but in WPF not. So How to return result from window, after it is closed?
Strings don’t work like that in C# – they are immutable.
You could get this to work using the
refkeyword as other people have suggested, however this will only work if you setSelectedItemin the constructor, which is a bit unlikely!The normal way of doing this is to have your dialog expose a property on your dialog:
This is the way that other dialogs (such as the open / save file dialogs) work.