WPF: create a dialog / invitation

I need to create a dialog / prompt, including a TextBox for user input. My problem is how to get the text after confirming the dialogue? Normally I would make a class for this that would save the text in the property. However, I want the Dialog design to use XAML. So I need to somehow extend the XAML code to keep the contents of the TextBox in the property, but I think this is not possible with pure XAML. What would be the best way to understand what I would like to do? How to create a dialog that can be determined from XAML, but can still somehow return the input? Thanks for any hint!

+60
wpf dialog prompt
May 9 '10 at 3:08
source share
4 answers

The “responsible” answer would be for me to suggest creating a ViewModel for the dialog and use two-way data binding in the TextBox so that the ViewModel has some ResponseText property or not. This is easy enough to do, but likely to survive.

A pragmatic answer would be to simply give your x: Name text field so that it becomes a member and expose the text as a property in your code behind the class, for example:

<!-- Incredibly simplified XAML --> <Window x:Class="MyDialog"> <StackPanel> <TextBlock Text="Enter some text" /> <TextBox x:Name="ResponseTextBox" /> <Button Content="OK" Click="OKButton_Click" /> </StackPanel> </Window> 

Then in your code behind ...

 partial class MyDialog : Window { public MyDialog() { InitializeComponent(); } public string ResponseText { get { return ResponseTextBox.Text; } set { ResponseTextBox.Text = value; } } private void OKButton_Click(object sender, System.Windows.RoutedEventArgs e) { DialogResult = true; } } 

Then, to use it ...

 var dialog = new MyDialog(); if (dialog.ShowDialog() == true) { MessageBox.Show("You said: " + dialog.ResponseText); } 
+109
May 09 '10 at 3:16 a.m.
source share

I just add a static method to call it as a MessageBox:

 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x:Class="utils.PromptDialog" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowStartupLocation="CenterScreen" SizeToContent="WidthAndHeight" MinWidth="300" MinHeight="100" WindowStyle="SingleBorderWindow" ResizeMode="CanMinimize"> <StackPanel Margin="5"> <TextBlock Name="txtQuestion" Margin="5"/> <TextBox Name="txtResponse" Margin="5"/> <PasswordBox Name="txtPasswordResponse" /> <StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Right"> <Button Content="_Ok" IsDefault="True" Margin="5" Name="btnOk" Click="btnOk_Click" /> <Button Content="_Cancel" IsCancel="True" Margin="5" Name="btnCancel" Click="btnCancel_Click" /> </StackPanel> </StackPanel> </Window> 

And the code behind:

 public partial class PromptDialog : Window { public enum InputType { Text, Password } private InputType _inputType = InputType.Text; public PromptDialog(string question, string title, string defaultValue = "", InputType inputType = InputType.Text) { InitializeComponent(); this.Loaded += new RoutedEventHandler(PromptDialog_Loaded); txtQuestion.Text = question; Title = title; txtResponse.Text = defaultValue; _inputType = inputType; if (_inputType == InputType.Password) txtResponse.Visibility = Visibility.Collapsed; else txtPasswordResponse.Visibility = Visibility.Collapsed; } void PromptDialog_Loaded(object sender, RoutedEventArgs e) { if (_inputType == InputType.Password) txtPasswordResponse.Focus(); else txtResponse.Focus(); } public static string Prompt(string question, string title, string defaultValue = "", InputType inputType = InputType.Text) { PromptDialog inst = new PromptDialog(question, title, defaultValue, inputType); inst.ShowDialog(); if (inst.DialogResult == true) return inst.ResponseText; return null; } public string ResponseText { get { if (_inputType == InputType.Password) return txtPasswordResponse.Password; else return txtResponse.Text; } } private void btnOk_Click(object sender, RoutedEventArgs e) { DialogResult = true; Close(); } private void btnCancel_Click(object sender, RoutedEventArgs e) { Close(); } } 

So you can call it like this:

 string repeatPassword = PromptDialog.Prompt("Repeat password", "Password confirm", inputType: PromptDialog.InputType.Password); 
+23
Jul 23 '13 at 13:29
source share

Josh’s excellent answer, all on credit, I slightly changed it to this:

Mydialog xaml

  <StackPanel Margin="5,5,5,5"> <TextBlock Name="TitleTextBox" Margin="0,0,0,10" /> <TextBox Name="InputTextBox" Padding="3,3,3,3" /> <Grid Margin="0,10,0,0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Button Name="BtnOk" Content="OK" Grid.Column="0" Margin="0,0,5,0" Padding="8" Click="BtnOk_Click" /> <Button Name="BtnCancel" Content="Cancel" Grid.Column="1" Margin="5,0,0,0" Padding="8" Click="BtnCancel_Click" /> </Grid> </StackPanel> 

MyDialog code for

  public MyDialog() { InitializeComponent(); } public MyDialog(string title,string input) { InitializeComponent(); TitleText = title; InputText = input; } public string TitleText { get { return TitleTextBox.Text; } set { TitleTextBox.Text = value; } } public string InputText { get { return InputTextBox.Text; } set { InputTextBox.Text = value; } } public bool Canceled { get; set; } private void BtnCancel_Click(object sender, System.Windows.RoutedEventArgs e) { Canceled = true; Close(); } private void BtnOk_Click(object sender, System.Windows.RoutedEventArgs e) { Canceled = false; Close(); } 

And call him somewhere else

 var dialog = new MyDialog("test", "hello"); dialog.Show(); dialog.Closing += (sender,e) => { var d = sender as MyDialog; if(!d.Canceled) MessageBox.Show(d.InputText); } 
+15
Jul 03 '13 at 9:47 on
source share

You do not need ANY of these other bizarre answers. The following is a simplified example that does not have all the Margin , Height , Width properties defined in XAML, but should be enough to show how to do this at a basic level.

Xaml
Create the Window page, as usual, and add your fields to it, say Label and TextBox inside the StackPanel :

 <StackPanel Orientation="Horizontal"> <Label Name="lblUser" Content="User Name:" /> <TextBox Name="txtUser" /> </StackPanel> 

Then create a standard Button for sending (“OK” or “Send”) and a “Cancel” button if you want:

 <StackPanel Orientation="Horizontal"> <Button Name="btnSubmit" Click="btnSubmit_Click" Content="Submit" /> <Button Name="btnCancel" Click="btnCancel_Click" Content="Cancel" /> </StackPanel> 

Code behind
You will add the Click event handler functions to the code behind, but when you go there, first declare a public variable in which you save your text field value:

 public static string strUserName = String.Empty; 

Then for the event handler functions (right-click on the Click function on the XAML button, select "Go to definition", it will create it for you), you need a check to see if your field is empty, you save it in your variable, if it is not, and close the window:

 private void btnSubmit_Click(object sender, RoutedEventArgs e) { if (!String.IsNullOrEmpty(txtUser.Text)) { strUserName = txtUser.Text; this.Close(); } else MessageBox.Show("Must provide a user name in the textbox."); } 

Call from another page
You think if I close my window with this this.Close() there, my value will disappear, right? NO!! I found this on another site: http://www.dreamincode.net/forums/topic/359208-wpf-how-to-make-simple-popup-window-for-input/

They had a similar example (I cleaned it up a bit) on how to open your Window from another and get the values:

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void btnOpenPopup_Click(object sender, RoutedEventArgs e) { MyPopupWindow popup = new MyPopupWindow(); // this is the class of your other page //ShowDialog means you can't focus the parent window, only the popup popup.ShowDialog(); //execution will block here in this method until the popup closes string result = popup.strUserName; UserNameTextBlock.Text = result; // should show what was input on the other page } } 

Cancel Button
You think, well, what about this Cancel button? Therefore, we simply add another public variable back to our popup code:

 public static bool cancelled = false; 

And turn on our btnCancel_Click event btnCancel_Click and make one change to btnSubmit_Click :

 private void btnCancel_Click(object sender, RoutedEventArgs e) { cancelled = true; strUserName = String.Empty; this.Close(); } private void btnSubmit_Click(object sender, RoutedEventArgs e) { if (!String.IsNullOrEmpty(txtUser.Text)) { strUserName = txtUser.Text; cancelled = false; // <-- I add this in here, just in case this.Close(); } else MessageBox.Show("Must provide a user name in the textbox."); } 

And then we just read this variable in our MainWindow btnOpenPopup_Click event:

 private void btnOpenPopup_Click(object sender, RoutedEventArgs e) { MyPopupWindow popup = new MyPopupWindow(); // this is the class of your other page //ShowDialog means you can't focus the parent window, only the popup popup.ShowDialog(); //execution will block here in this method until the popup closes // **Here we find out if we cancelled or not** if (popup.cancelled == true) return; else { string result = popup.strUserName; UserNameTextBlock.Text = result; // should show what was input on the other page } } 

Long response, but I wanted to show how easy it is to use public static variables. No DialogResult , no return values, nothing. Just open the window, save your values ​​using the button events in the popup window, and then run them in the main window functions.

+2
Nov 09 '16 at 20:57
source share



All Articles