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();
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;
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();
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.