Home > Mobile >  WPF Add Text in Page1, Navigate to Page2, and See it in Page2
WPF Add Text in Page1, Navigate to Page2, and See it in Page2

Time:09-07

I need help transferring text from one textbox in one page to another textbox in another page since I'm new to WPF. I was able to change pages, but I can't get the textbox from the first page into the textbox in the second page. It just changes pages but doesn't add in the value from the 1st textbox. I've been looking for help for hours but can't make it work out.

Page1.xaml:

<TextBox x:Name = "ATextBox"/>

Page1.cs:

            private void AppealNumberTextBoxEnter(object sender, KeyEventArgs e)
    {
        //When Enter Key is pushed in textbox
        if (e.Key == Key.Return)
        {NavigationService ns = NavigationService.GetNavigationService(this);
            ns.Navigate(new Uri("Page2.xaml", UriKind.Relative));}

            Page2 p2 = new Page2();
            p2.BTextBox.Text = ATextBox.Text;

Page2.xmal:

<TextBox x:Name = "BTextBox"/>

CodePudding user response:

Your code doesn't work because you modify the Text of a TextBox of a page you just created, but that page isn't the same as the one that was created with the call to Navigate().
What you can do is create the Page2, then navigate to it, such as :

if (e.Key == Key.Return)
{
    Page2 p2 = new Page2();
    p2.BTextBox.Text = ATextBox.Text;

    // Note: if you are in Page.xaml.cs, I think you can simplify
    // 'NavigationService.GetNavigationService(this)' to just 'NavigationService'
    NavigationService.Navigate(p2);
}

I think it is also possible to keep your current code with a small change :

if (e.Key == Key.Return)
{
    NavigationService ns = NavigationService.GetNavigationService(this);
    ns.Navigate(new Uri("Page2.xaml", UriKind.Relative));

    Page2 p2 = (Page2)ns.Content;
    p2.BTextBox.Text = ATextBox.Text;
}

Note that I moved the text change inside the if condition, as there is no need to update the text of the TextBox in page 2 if it is not displayed.

CodePudding user response:

Since you have just started learning WPF, I advise you to immediately learn the correct implementation. The concept of WPF is built around UI elements getting the data they need on their own. To do this, you need to create a data source object to the properties of which the properties of UI elements will be bound. In this implementation, all data is stored in this source object, and updating its properties with any element updates all other bound properties as well.

In the simplest case, it's like this:

    public class MyProxy
    {
        public string SomeText {get; set;}
    }

In App Resources:

    <Application.Resources>
        <vms:MyProxy x:Key="proxy"/>
    </Application.Resources>

vms: is the namespace prefix of the MyProxy class.

In Pages:

    <TextBox Text="{Binding SomeText, Source={StaticResource proxy}}"/>

When implementing MVVM (and this is very typical for WPF), such a data source is called a ViewModel and is passed to the DataContext of the Window and/or Page. In this case, bindings are simplified to:

    <TextBox Text="{Binding SomeText}"/>

If the data source properties are supposed to be changed from the Sharp code, then the INotifyPropertyChanged interface must be implemented in the data source. Here is my simple implementation: BaseInpc. And the data source based on it:

    public class MyViewModel : BaseInpc
    {
        private string _someText;
        public string SomeText 
        {
            get => _someText;
            set => Set(ref _someText, value);
        }
    }
  • Related