title: Passing Page1 Text to Page 2 WPF MVVM
body: <p>I have been all over the internet looking for a clear answer and cannot find one.  I have created Window Forms apps in the past, and decided to give WPF a try.  I am hoping I don't have to move back to WinForms, but cannot figure out the simplest tasks.  </p>
  
  <p>How do I pass text in Page1's Textbox to Page2's TextBox.</p>
  
  <p>I apologize if this is a duplicate, but I cannot find a clear answer anywhere.  Most beginner mvvm tutorials seem to stick to one page apps.</p>
  
  <p>MainWindo.xaml</p>
  
  <pre><code>&lt;Window x:Class="WpfApp3.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:local="clr-namespace:WpfApp3"
          mc:Ignorable="d"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Grid&gt;
          &lt;Frame Source="/Page1.xaml"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Page1 Xaml:</p>
  
  <pre><code>&lt;Page x:Class="WpfApp3.Page1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d" 
        d:DesignHeight="300" d:DesignWidth="300"
        Title="Page1"
        xmlns:vm="clr-namespace:WpfApp3"&gt;
      &lt;Page.DataContext&gt;
          &lt;vm:Page1ViewModel/&gt;
      &lt;/Page.DataContext&gt;
      &lt;Grid&gt;
  
          &lt;StackPanel&gt;
              &lt;TextBlock Margin="20" FontSize="36"&gt;Welcome Home&lt;/TextBlock&gt;
              &lt;TextBox x:Name="UserName" Margin="10 0 10 0" Text=""/&gt;
              &lt;Button x:Name="Next" Margin="10 10" Content="Next" Click="Next_Click"/&gt;
          &lt;/StackPanel&gt;
  
      &lt;/Grid&gt;
  &lt;/Page&gt;
  </code></pre>
  
  <p>Page1 Code-Behind:</p>
  
  <pre><code>using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace WpfApp3
  {
      /// &lt;summary&gt;
      /// Interaction logic for Page1.xaml
      /// &lt;/summary&gt;
      public partial class Page1 : Page
      {
          public Page1()
          {
              InitializeComponent();
          }
          private void Next_Click(object sender, RoutedEventArgs e)
          {
              NavigationService.Navigate(
                  new Uri("/Page2.xaml", UriKind.Relative));
          }
      }
  }
  </code></pre>
  
  <p>Page1 ViewModel:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace WpfApp3
  {
      class Page1ViewModel : Notifier
      {
          private string username;
          public string UserName
          {
              get { return username; }
              set
              {
                  username = value;
                  OnPropertyChanged("UserName");
              }
          }
  
      }
  }
  </code></pre>
  
  <p>Page2 Xaml:</p>
  
  <pre><code>&lt;Page x:Class="WpfApp3.Page2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d" 
        d:DesignHeight="300" d:DesignWidth="300"
        Title="Page2"
        xmlns:vm="clr-namespace:WpfApp3&gt;
        &lt;Page.DataContext&gt;
            &lt;vm:Page2ViewModel/&gt;
        &lt;Page.Data.Context&gt;
  
  
      &lt;Grid&gt;
          &lt;StackPanel&gt;
              &lt;TextBlock Margin="10"&gt;Hello there: &lt;/TextBlock&gt;
              &lt;TextBox x:Name="TextBox_Name" Margin="10" Text="{Binding UserName}"/&gt;
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  &lt;/Page&gt;
  </code></pre>
  
  <p>Page2 Code-behind:</p>
  
  <pre><code>using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace WpfApp3
  {
      /// &lt;summary&gt;
      /// Interaction logic for Page2.xaml
      /// &lt;/summary&gt;
      public partial class Page2 : Page
      {
          public Page2()
          {
              InitializeComponent();   
          }
      }
  }
  </code></pre>
  
  <p>Page2 View Model:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace WpfApp3
  {
      class Page2ViewModel : Notifier
      {
          private string textbox_name;
          public string TextBox_Name
          {
              get { return textbox_name; }
              set
              {
                  textbox_name = value;
                  OnPropertyChanged("TextBox_Name");
              }
          }
      }
  }
  </code></pre>