user995387
user995387

Reputation: 385

How to get result from custom WPF window to parent window in UserControl

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?

Upvotes: 0

Views: 1371

Answers (3)

Gayot Fow
Gayot Fow

Reputation: 8812

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.

Source: http://blogs.msdn.com/b/marthami/archive/2007/10/02/how-to-use-pagefunction-to-create-dialog-behavior-in-wpf.aspx

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...

WPF - PageFunctions. Why are they needed?

Upvotes: 1

Muad'Dib
Muad'Dib

Reputation: 29266

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...
}

Upvotes: 2

SLaks
SLaks

Reputation: 888167

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.

Upvotes: 1

Related Questions