title: User input not being assigned to variable? (C# with WPF)
body: <p>I'm making an application in which the user inputs their desired name, which is validated before being assigned to a playerName string variable. For some reason, the input isn't being assigned to the variable, so when I run the program, a blank space shows where the name should be in blocks of text. I have no idea what's causing this. I'm using two separate windows, InputWindow and GameWindow, for the applicaton.</p>
  
  <p>The XAML and codebehind for InputWindow are as follows:</p>
  
  <pre><code>&lt;Window x:Class="COMP4_Project.InputWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="New Game" Height="120" Width="250" ResizeMode="CanMinimize"&gt;
  &lt;Grid Margin="10"&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width="Auto" /&gt;
          &lt;ColumnDefinition Width="*" /&gt;
      &lt;/Grid.ColumnDefinitions&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="Auto" /&gt;
          &lt;RowDefinition Height="Auto" /&gt;
  
      &lt;/Grid.RowDefinitions&gt;
      &lt;Label&gt;Enter name:&lt;/Label&gt;
      &lt;TextBox x:Name="textInput" Grid.Column="2" Margin="0,0,0,0" MaxLength="10"&gt;&lt;/TextBox&gt;
      &lt;Button x:Name="Confirm" Click="Confirm_Click" Grid.Row="2" Width="80" Height="25" Content="Confirm" Margin="0,10,0,41" /&gt;
      &lt;Button x:Name="Cancel" Click="Cancel_Click" Grid.Row="2" Grid.Column="1" Width="80" Height="25" Content="Cancel" HorizontalAlignment="Right" Margin="54,10,0,41" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Text.RegularExpressions;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Shapes;
  
  namespace COMP4_Project
  {
  public class WorldVariables
  {
      public int[] worldLocale = { 0, 1, 2, 3 };
      public string worldName;
      public int playerScore = 0;
      public string playerName;
  }
  
  /// &lt;summary&gt;
  /// Interaction logic for InputWindow.xaml
  /// &lt;/summary&gt;
  public partial class InputWindow : Window
  {
      private readonly WorldVariables worldVariables = new WorldVariables();
  
      public InputWindow()
      {
          InitializeComponent();
      }
  
      private void Confirm_Click(object sender, RoutedEventArgs e)
      {
          // Text entered into the "textInput" textbox is assigned to the userInput variable
          string userInput = textInput.Text;
  
          // Validation for user input using regular expressions library
          Regex r = new Regex("^[a-zA-Z]+$");
          if (r.IsMatch(userInput))
          {
              // Assigns text held in userInput variable to playerName variable
              worldVariables.playerName = userInput;
              // Close input window
              Close();
              // Opens game window
              Window win = new GameWindow();
              // Sets owner of game window as the main window
              win.Owner = Application.Current.MainWindow;
              win.ShowDialog();
          } 
          else
          {
              // Message informing user that input was invalid
              MessageBox.Show("Only letters permitted; name field can't be empty. Try again!");
          }
      }
  
      private void Cancel_Click(object sender, RoutedEventArgs e)
      {
          // Close input window
          Close();
      }
  }
  }
  </code></pre>
  
  <p>And this is the XAML and codebehind for GameWindow, up to the first instance where the playerName string is used in text.</p>
  
  <pre><code>&lt;Window x:Class="COMP4_Project.GameWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Psych" Height="400" Width="500" ResizeMode="CanMinimize"&gt;
  &lt;Grid Margin="10"&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width="Auto" /&gt;
          &lt;ColumnDefinition Width="50" /&gt;
          &lt;ColumnDefinition Width="50" /&gt;
          &lt;ColumnDefinition Width="50" /&gt;
          &lt;ColumnDefinition Width="180"/&gt;
          &lt;ColumnDefinition Width="75" /&gt;
          &lt;ColumnDefinition Width="75" /&gt;
      &lt;/Grid.ColumnDefinitions&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="Auto" /&gt;
          &lt;RowDefinition Height="17" /&gt;
          &lt;RowDefinition Height="113" /&gt;
          &lt;RowDefinition Height="80" /&gt;
          &lt;RowDefinition Height="33" /&gt;
          &lt;RowDefinition Height="33"/&gt;
          &lt;RowDefinition Height="17" /&gt;
          &lt;RowDefinition Height="17" /&gt;
          &lt;RowDefinition Height="17" /&gt;
          &lt;RowDefinition Height="66" /&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;TextBlock x:Name="WorldName" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="3" TextWrapping="NoWrap" TextAlignment="Left" Text="World: Tunnel Entrance" /&gt;
      &lt;TextBlock x:Name="ScoreTracker" Grid.Column="5" Margin="0,0,0,0" TextWrapping="Wrap" TextAlignment="Left" Text="Score:" /&gt;
      &lt;TextBlock x:Name="ScoreCount" Grid.Column="6" Margin="0,0,0,0" TextWrapping="Wrap" TextAlignment="Left" Text="0" /&gt;
      &lt;TextBlock x:Name="Dialogue"  Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="5" TextWrapping="Wrap" TextAlignment="Justify" Text="Where do you want to go? Click on the options below to change your location or click on an NPC to talk to them." /&gt;
      &lt;TextBlock x:Name="Option1" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="6" MouseLeftButtonDown="Option1_MouseLeftButtonDown" Text="   &gt; Go Left" /&gt;
      &lt;TextBlock x:Name="Option2" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="7" MouseLeftButtonDown="Option2_MouseLeftButtonDown" Text="   &gt; Go Right" /&gt;
      &lt;TextBlock x:Name="Option3" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="8" MouseLeftButtonDown="Option3_MouseLeftButtonDown" Text="   &gt; Enter Tunnel" /&gt;
      &lt;TextBlock x:Name="Option4" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="6" MouseLeftButtonDown="Option4_MouseLeftButtonDown"  Text="   &gt; Go Back" Visibility="Hidden" /&gt;
      &lt;Image x:Name="Portrait" Source="C:\Psyche\Images\red1.png" Grid.Column="4" Grid.Row="1" Grid.RowSpan="4" Visibility="Hidden" /&gt;
      &lt;Image x:Name="Sprite" Source="C:\Psyche\Images\redsprite1.png" Grid.Column="2" Grid.Row="3" MouseLeftButtonDown="Sprite_MouseLeftButtonDown" /&gt;
      &lt;TextBlock x:Name="OptionBegin" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="6" MouseLeftButtonDown="OptionBegin_MouseLeftButtonDown" Text="   &gt; Begin Test" Visibility="Hidden" /&gt;
      &lt;TextBlock x:Name="Q1A1" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="6" MouseLeftButtonDown="Q1A1_MouseLeftButtonDown" Text="   &gt; Divergence from Standard Normality" Visibility="Hidden" /&gt;
      &lt;TextBlock x:Name="Q1A2" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="7" MouseLeftButtonDown="Q1A2_MouseLeftButtonDown" Text="   &gt; Deviation from Social Norms" Visibility="Hidden" /&gt;
      &lt;TextBlock x:Name="Q1A3" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="8" MouseLeftButtonDown="Q1A3_MouseLeftButtonDown" Text="   &gt; Damaging Social Negligence" Visibility="Hidden" /&gt;
      &lt;TextBlock x:Name="Q1Cont" Grid.Column="1" Margin="0,0,0,0" Grid.ColumnSpan="6" Grid.Row="6" MouseLeftButtonDown="Q1Cont_MouseLeftButtonDown" Text="   &gt; Next question" Visibility="Hidden" /&gt;
  </code></pre>
  
  <p>
  </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Shapes;
  
  namespace COMP4_Project
  {
  
  /// &lt;summary&gt;
  /// Interaction logic for GameWindow.xaml
  /// &lt;/summary&gt;  
  
  public partial class GameWindow : Window
  {
      private readonly WorldVariables worldVariables = new WorldVariables();
  
      public GameWindow()
      {
          InitializeComponent();
  
          // Set background image for window
          ImageBrush myBrush = new ImageBrush();
          myBrush.ImageSource =
              new BitmapImage(new Uri(@"C:\Psyche\Images\background0.png", UriKind.Absolute));
          this.Background = myBrush;
  
          // Ensures non-player character sprite is visible on launching the game window
          Sprite.Visibility = Visibility.Visible;
      }
  
      private void Option1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {
          // Change background image
          this.Background = new ImageBrush(new BitmapImage(new Uri(@"C:\Psyche\Images\background1.png")));
          // Hide character sprite
          Sprite.Visibility = Visibility.Hidden;
          // Hide character portrait (should not be visible regardless, this is simply a failsafe)
          Portrait.Visibility = Visibility.Hidden;
          // Change visible options
          Option1.Visibility = Visibility.Hidden;
          Option2.Visibility = Visibility.Hidden;
          Option3.Visibility = Visibility.Hidden;
          Option4.Visibility = Visibility.Visible;
          // Alter WorldName field to match change in scene
          WorldName.Text = "Location: Stream";
          // Alter dialogue to match change in scene
          Dialogue.Text = "There's a tree across the stream. Not much else around, though.";
  
      }
  
      private void Option2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {
          this.Background = new ImageBrush(new BitmapImage(new Uri(@"C:\Psyche\Images\background2.png")));
          Sprite.Visibility = Visibility.Hidden;
          Portrait.Visibility = Visibility.Hidden;
          Option1.Visibility = Visibility.Hidden;
          Option2.Visibility = Visibility.Hidden;
          Option3.Visibility = Visibility.Hidden;
          Option4.Visibility = Visibility.Visible;
          WorldName.Text = "Location: Forest";
          Dialogue.Text = "It's pretty dark here due to the dense canopy of trees overhead. Kind of creepy when you think about it.";
      }
  
      private void Option3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {
          this.Background = new ImageBrush(new BitmapImage(new Uri(@"C:\Psyche\Images\background3.png")));
          Sprite.Visibility = Visibility.Hidden;
          Portrait.Visibility = Visibility.Hidden;
          Option1.Visibility = Visibility.Hidden;
          Option2.Visibility = Visibility.Hidden;
          Option3.Visibility = Visibility.Hidden;
          Option4.Visibility = Visibility.Visible;
          WorldName.Text = "Location: Tunnel";
          Dialogue.Text = "There's nothing here, except a small dot of light in the distance. That might be the exit, but it's too far to check safely.";
      }
  
      private void Option4_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {
          this.Background = new ImageBrush(new BitmapImage(new Uri(@"C:\Psyche\Images\background0.png")));
          Sprite.Visibility = Visibility.Visible;
          Portrait.Visibility = Visibility.Hidden;
          Option1.Visibility = Visibility.Visible;
          Option2.Visibility = Visibility.Visible;
          Option3.Visibility = Visibility.Visible;
          Option4.Visibility = Visibility.Hidden;
          // Revert worldname to previous text
          WorldName.Text = "Location: Tunnel Entrance";
          // Revert dialogue to previous text
          Dialogue.Text = "Where do you want to go? Click on the options below to change your location or click on an NPC to talk to them.";
      }
  
      private void Sprite_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {
          // Hide character sprite
          Sprite.Visibility = Visibility.Hidden;
          // Show character portrait
          Portrait.Visibility = Visibility.Visible;
          // Hide all previous options to avoid overlap
          Option1.Visibility = Visibility.Hidden;
          Option2.Visibility = Visibility.Hidden;
          Option3.Visibility = Visibility.Hidden;
          Option4.Visibility = Visibility.Hidden;
          // Dialogue text changes to show NPC dialogue
          Dialogue.Text = "Red: Oh, hey " + worldVariables.playerName + ", you want to revise your knowledge?";
          // Show option to begin test
          OptionBegin.Visibility = Visibility.Visible;
      }
  </code></pre>
  
  <p>Everything works fine aside from this bug, so any help fixing it would be appreciated.</p>