简体   繁体   中英

C# WPF : how can i access page controls from another page to get textbox.text

Hello i just started learning WPF and i'm confused. i have 2 pages i want to get TextBox.text value form page2 how? in page1.xaml.cs:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Page2 p = new Page2();
        MessageBox.Show(p.textbox1.Text);
    }

the code i provided when i click the button messagebox show up empty and textbox1 contains text.

please explain how can i access p.textbox1.Text value in correct way.

If I understund well, you want to pass a value from a class to other class. So you should create a method that returs your value like this:

In page 1

   Page2 p = new Page2();
   p.getTextBox();

In page 2

    public string getTextBox(){
      return  TextBox.text ;
     }

Also, you can work with static variables or a file xml to save your string. I hope taht it helped you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM