title: C# User Must Add Multiple Buttons to WPF and Buttons Must Persist
body: <p>I am attempting to create a program in which the User can create multiple profiles. These profiles can be accessed via buttons that appear as each profile is completed. </p>
  
  <p>My problem:</p>
  
  <ul>
  <li>I have no clue how to make the created buttons persist after the program is exited(I need to save the buttons?)</li>
  </ul>
  
  <p>Visually, this is program's process: 1) Enter your information, click continue 2) View a display page of what you entered, click done. 3) This adds a button to the final window, the button of course takes you to 4) Your profile you just created.  </p>
  
  <p><img src="https://i.stack.imgur.com/VtKfH.png" alt="You begin the form by entering the information.">
  <img src="https://i.stack.imgur.com/2nvi6.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/DcMHD.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/iSWsw.png" alt="enter image description here"></p>
  
  <p>After this, the program ends and nothing is saved. I'm fairly new to c# and am quite confused on how to "save" multiple buttons without massively complicating the code. I'm a complete noob to c# and have a little Java experience. Am I going about this correctly? I'm pretty sure its possible but have no idea to go about it. </p>
  
  <p>I will include my code below. I'm working in visual studios 2012. any help would be appreciated!
  MainWindow XAML:</p>
  
  <pre><code>&lt;Window x:Class="VendorMain.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525"&gt;
  
  &lt;Grid&gt;
      &lt;Label Content="FirstName" HorizontalAlignment="Left" Margin="63,45,0,0" VerticalAlignment="Top"/&gt;
      &lt;Label Content="LastName" HorizontalAlignment="Left" Margin="63,71,0,0" VerticalAlignment="Top"/&gt;
      &lt;Label Content="Image" HorizontalAlignment="Left" Margin="63,102,0,0" VerticalAlignment="Top"/&gt;
  
      &lt;Image Name="imgPhoto" Stretch="Fill" Margin="63,133,303,69"&gt;&lt;/Image&gt;
      &lt;Button Name="UploadImageButton" Content="Upload Image" HorizontalAlignment="Left" Margin="130,105,0,0" VerticalAlignment="Top" Width="84" Click="UploadImageButton_Click"/&gt;
  
      &lt;TextBox Name="AssignFirstName" Text="{Binding SettingFirstname}" HorizontalAlignment="Left" Height="23" Margin="130,48,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" /&gt;
      &lt;TextBox Name="AssignLastName" Text="{Binding SettingLastName}" HorizontalAlignment="Left" Height="23" Margin="130,75,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/&gt;
  
      &lt;Button Name="ContinueToDisplayWindow" Content="Continue" HorizontalAlignment="Left" Margin="409,288,0,0" VerticalAlignment="Top" Width="75" Click="ContinueToDisplayWindow_Click" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>MainWindow Code:</p>
  
  <pre><code>namespace VendorMain
  {
      public partial class MainWindow : Window
      {
      public MainWindow()
      {
          InitializeComponent();
      }
  
      private void UploadImageButton_Click(object sender, RoutedEventArgs e)
      {
          OpenFileDialog op = new OpenFileDialog();
          op.Title = "Select a picture";
          op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
              "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
              "Portable Network Graphic (*.png)|*.png";
          if (op.ShowDialog() == true)
          {
              imgPhoto.Source = new BitmapImage(new System.Uri(op.FileName));
              //SettingImage.Source = imgPhoto.Source; 
          }
      }
  
      private void ContinueToDisplayWindow_Click(object sender, RoutedEventArgs e)
      {
          DisplayPage displaypg = new DisplayPage();
          displaypg.DpFirstName.Content = AssignFirstName.Text;
          displaypg.DpLastName.Content = AssignLastName.Text;
          displaypg.DpImage.Source = imgPhoto.Source;
          displaypg.Show();
      }      
  }
  }
  </code></pre>
  
  <p>DisplayPage XAML:</p>
  
  <pre><code> &lt;Window x:Class="VendorMain.DisplayPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="DisplayPage" Height="300" Width="525"&gt;
  &lt;Grid&gt;
      &lt;Label Name="DpFirstName" Content="{Binding getFirstNamePermenent}" HorizontalAlignment="Left" Margin="86,55,0,0" VerticalAlignment="Top"/&gt;
      &lt;Label Name="DpLastName" Content="{Binding getLastNamePermenent}" HorizontalAlignment="Left" Margin="87,80,0,0" VerticalAlignment="Top"/&gt;
      &lt;Image Name="DpImage" HorizontalAlignment="Left" Height="100" Margin="94,111,0,0" VerticalAlignment="Top" Width="100"/&gt;
      &lt;Button Name="ButtonizeThisProfile_Button" Content="Done" HorizontalAlignment="Left" Margin="420,238,0,0" VerticalAlignment="Top" Width="75" Click="ButtonizeThisProfile_Button_Click"/&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>   </p>
  
  <p>DisplayPage Code:</p>
  
  <pre><code>namespace VendorMain
  {
  /// &lt;summary&gt;
  /// Interaction logic for DisplayPage.xaml
  /// &lt;/summary&gt;
  public partial class DisplayPage : Window
  {
      public Button bot1;
  
      public DisplayPage()
      {
          InitializeComponent();
      }
  
      private void newBtn_Click(object sender, RoutedEventArgs e)
      {
          carryToFinalView();
      }
  
      private void ButtonizeThisProfile_Button_Click(object sender, RoutedEventArgs e)
      {
          UserProfiles uPro = new UserProfiles();
  
          System.Windows.Controls.Button newBtn = new Button();
          newBtn.Content = "Person1";
          newBtn.Name = "NewProfileButtonAccess";
          newBtn.Click += new RoutedEventHandler(newBtn_Click);
          uPro.ButtonArea.Children.Add(newBtn);
          uPro.Show();
      }
  
      public void carryToFinalView()
      {
          DisplayPage displaypg = new DisplayPage();
          displaypg.DpFirstName.Content = DpFirstName.Content;
          displaypg.DpLastName.Content = DpLastName.Content;
          displaypg.DpImage.Source = DpImage.Source;
          displaypg.Show();
      }
  }
  }
  </code></pre>
  
  <p>UserProfile XAML:</p>
  
  <pre><code>&lt;Window x:Class="VendorMain.UserProfiles"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="UserProfiles" Height="300" Width="525"&gt;
  &lt;Grid&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width=".8*" /&gt;
          &lt;ColumnDefinition Width="2*"/&gt;
      &lt;/Grid.ColumnDefinitions&gt;
  
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="6*"/&gt;
          &lt;RowDefinition Height="11*"/&gt;
      &lt;/Grid.RowDefinitions&gt;
  
      &lt;Label Content="User Profiles: " HorizontalAlignment="Left" Margin="37,47,0,0" VerticalAlignment="Top"/&gt;
  
      &lt;StackPanel Name="ButtonArea" Grid.Column="2" Grid.Row="2"&gt;
  
      &lt;/StackPanel&gt;
      &lt;Button Name="AddAnotherProfileButton" Content="Button" HorizontalAlignment="Left" Margin="35,146,0,0" Grid.Row="1" VerticalAlignment="Top" Width="75" Click="AddAnotherProfileButton_Click"/&gt;
  
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>UserProfile Code:</p>
  
  <pre><code>namespace VendorMain
  {
  
  public partial class UserProfiles : Window
  {
      public UserProfiles()
      {
          InitializeComponent();
      }
  
      private void AddAnotherProfileButton_Click(object sender, RoutedEventArgs e)
      {
          MainWindow mw = new MainWindow();
          mw.Show();
      }
  }
  }
  </code></pre>