(WPF) window pass parameters between

Source: Internet
Author: User

Https://www.cnblogs.com/fdyang/archive/2013/03/25/2980451.html

There are several ways:

1. Declare a global variable, that is, App.xaml inside the declaration, in all forms can be referenced application.current.properties["ArgumentName"];

2. The second is to expose a property on the target form, directly assigning a value;

3. Finally, the parameter navigationservice.navigate (window object,argument value) is passed inside the URI.

4. Use event response to pass the value.

For example: Click on a Opensubwindow button on the main window MainWindow-> Open the child window Subwindow-> Enter the value in the TextBox in the child window, click OK and then close the-> The textbox on the main window gets the value in the child window.

1. Define an event passvaluesevent in the child window. When the OK button is clicked, the event is triggered and the value is passed. (Passvalueseventargs is a EventArgs class that needs to be defined at the same time.)

    public partial class Subwindow:window
    {public
        delegate void Passvalueshandler (object sender, Passvalueseventargs e);

        public event Passvalueshandler Passvaluesevent; 

        Public Subwindow ()
        {
            InitializeComponent ();
        }

        private void Btnok_click (object sender, RoutedEventArgs e)
        {
            string value1 = Tbvalue1.text;   The Text property return value is String type.
            int value2;
            Int32.TryParse (Tbvalue2.text, out value2);

            Passvalueseventargs args = new Passvalueseventargs (value1, value2);
            Passvaluesevent (this, args);

            This. Close ();
        }
    }

2. In the main window of the Opensubwindow button click Method, subscribed to the Passvaluesevent event. Gets the value of the passed parameter when the event is triggered.

    public partial class Mainwindow:window
    {public
        MainWindow ()
        {
            InitializeComponent ();
        }

        private void Btnopensubwindow_click (object sender, RoutedEventArgs e)
        {
            Subwindow subwindow = new Subwindow (); c9/>//Subscription Event
            Subwindow.passvaluesevent + + new Subwindow.passvalueshandler (receivevalues);

            Subwindow.show (); 
        }

        private void Receivevalues (object sender, Passvalueseventargs e)
        {
            this.tbValue1.Text = e.value1;
            This.tbValue2.Text = E.value2.tostring (); 
        }
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.