GirishK
GirishK

Reputation: 1963

Opening WPF form as a dialog on button click

I am building a WPF application in which I need to open one of my WPF forms as a dialog (pop up) on the button click of another form. I know how do it in windows forms, just not getting how I'll do it in WPF.

Thanks in advance.

Upvotes: 1

Views: 8313

Answers (2)

brunnerh
brunnerh

Reputation: 184524

var diag = new Dialog();
diag.Show(); // or diag.ShowDialog(); for a modal dialog. Returns a 'bool?'

Dialog is a class you have to create yourself, it should inherit from Window, which has the methods Show and ShowDialog. (In Visual Studio you best use the Window-template for window creation so the XAML markup file which belongs to the dialogue is created automatically)

You can of course create a dialogue on the fly as well. e.g.

var dialog = new Window();
var sp = new StackPanel();
sp.Children.Add(new TextBlock(new Run("This is some text")));
var button = new Button();
button.Content = "OK";
button.Click += (s,e) => dialog.DialogResult = true;
sp.Children.Add(button);
dialog.Content = sp;
dialog.ShowDialog();

(Code written right here, may have errors)

Upvotes: 0

IAmTimCorey
IAmTimCorey

Reputation: 16757

Here is a complete explanation of how to do a Dialog in WPF:

http://marlongrech.wordpress.com/2008/05/28/wpf-dialogs-and-dialogresult/

The basic code you are looking for is as follows:

wpfDialog dialog = new wpfDialog();
dialog.ShowDialog();

The above article will walk you through how to get information back from the form if you want it as well.

Upvotes: 1

Related Questions