开发者

Opening WPF form as a dialog on button click

开发者 https://www.devze.com 2023-03-10 05:53 出处:网络
I am bui开发者_JS百科lding 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 h

I am bui开发者_JS百科lding 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.


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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消