WPF: Create a dialog / prompt

前端 未结 4 440
执念已碎
执念已碎 2020-12-12 13:42

I need to create a Dialog / Prompt including TextBox for user input. My problem is, how to get the text after having confirmed the dialog? Usually I would make a class for t

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-12 14:04

    You don't need ANY of these other fancy answers. Below is a simplistic example that doesn't have all the Margin, Height, Width properties set in the XAML, but should be enough to show how to get this done at a basic level.

    XAML
    Build a Window page like you would normally and add your fields to it, say a Label and TextBox control inside a StackPanel:

    
        
    

    Then create a standard Button for Submission ("OK" or "Submit") and a "Cancel" button if you like:

    
        

    Code-Behind
    You'll add the Click event handler functions in the code-behind, but when you go there, first, declare a public variable where you will store your textbox value:

    public static string strUserName = String.Empty;
    

    Then, for the event handler functions (right-click the Click function on the button XAML, select "Go To Definition", it will create it for you), you need a check to see if your box is empty. You store it in your variable if it is not, and close your 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.");
    }
    

    Calling It From Another Page
    You're thinking, if I close my window with that this.Close() up there, my value is gone, right? NO!! I found this out from another site: http://www.dreamincode.net/forums/topic/359208-wpf-how-to-make-simple-popup-window-for-input/

    They had a similar example to this (I cleaned it up a bit) of how to open your Window from another and retrieve 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're thinking, well what about that Cancel button, though? So we just add another public variable back in our pop-up window code-behind:

    public static bool cancelled = false;
    

    And let's include our btnCancel_Click event handler, 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 that 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 this is using public static variables. No DialogResult, no returning values, nothing. Just open the window, store your values with the button events in the pop-up window, then retrieve them afterwards in the main window function.

提交回复
热议问题