How to use WPF window as a messagebox? here is how I was able to get messagebox. Now I want it to return back certain value in the userControl. Any help?
3 Answers
as @SLaks says, use the DialogReslult... if that is not enough and you are using an MVVM model, then you could use your data model: set the DataContext
of the child window to your data model instance then you can bind the contorls in your child window to any data member on your model--typically you would set the DataContext
to the DataContext
of the parent window...
protected popMyWindow()
{
MyChildWindow cw = new MyChildWindow();
cw.DataContext = this.DataContext();
// show the window...
}

- 28,542
- 5
- 55
- 68
Set the window's DialogResult
in the window itself before closing.
The value you set will be returned by ShowDialog()
If you want to return more than a bool?
, create a wrapper method that calls ShowDialog()
and returns whatever you want.

- 868,454
- 176
- 1,908
- 1,964
The WPF solution for these problems is the 'Page Function'.
PageFunction is a new term defined in WPF. It enables the user to navigate to a specific page and perform a task, then navigate back to the caller page with the result. It behaves just like Modal Dialogbox with the difference that PageFunction won’t be displayed as s pop-up, instead it is displayed in the same page as the caller.
It differs from the pattern of wrapping the ShowDialog in that the page is navigated to, and more importantly, it is already strongly typed within the WPF plumbing and does not require you to develop a new class to do the same thing.
There is an explanatory StackOverflow thread here...
-
1this is good, only caveat is that the app needs to be a "navigation application" AFIKT – Muad'Dib Dec 27 '11 at 17:27
-
Yes, presumably the WPF forefathers had it in mind that if you needed to get something back from a dialog, you were implicitly in a navigation context anyway. And here's thought: expect to see the PageFunction come up *several* times in different contexts in the 502 certification exam. That's why I used the phrase "The WPF solution" :) – Gayot Fow Dec 27 '11 at 19:31