title: How to dynamically access element names in XAML?
body: <p>I have a XAML input form which the user fills out.</p>
  
  <p>I want to validate this form.</p>
  
  <p>I have the field information in a collection which I want to loop through and check each field.</p>
  
  <p>But how do I access the name of the field when it is in a string, <strong>e.g. when fieldInformation.FieldName = "CompanyName" I want to check "Field_CompanyName.Text".</strong></p>
  
  <p>Pseudocode:</p>
  
  <pre><code>foreach (var fieldInformation in _fieldInformations)
  {
      if (Field_{&amp;fieldInformation.FieldName}.Text.Length &gt; 2)
      {
          ErrorMessage.Text = String.Format("The length of {0} is too long, please correct.", fieldInformation.FieldName);
          entryIsValid = false;
      }
  }
  </code></pre>
  
  <p><strong>XAML:</strong></p>
  
  <pre><code>&lt;StackPanel Orientation="Horizontal" Margin="10 10 10 0"&gt;
      &lt;TextBlock Width="150" Text="Customer ID:"/&gt;
      &lt;TextBox x:Name="Field_CustomerID" Width="150" MaxLength="5" Text=""/&gt;
  &lt;/StackPanel&gt;
  &lt;StackPanel Orientation="Horizontal" Margin="10 10 10 0"&gt;
      &lt;TextBlock Width="150" Text="Company Name:"/&gt;
      &lt;TextBox x:Name="Field_CompanyName" Width="150" MaxLength="40" Text=""/&gt;
  &lt;/StackPanel&gt;
  &lt;StackPanel Orientation="Horizontal" Margin="10 10 10 0"&gt;
      &lt;TextBlock Width="150"  Text="Contact Name:"/&gt;
      &lt;TextBox x:Name="Field_ContactName" Width="150" MaxLength="30" Text=""/&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p><strong>Code-Behind:</strong></p>
  
  <pre><code>_fieldInformations.Add(new FieldInformation { FieldName = "CustomerID", FieldSize = 5 });
  _fieldInformations.Add(new FieldInformation { FieldName = "CompanyName", FieldSize = 40 });
  _fieldInformations.Add(new FieldInformation { FieldName = "ContactName", FieldSize = 30 });
  </code></pre>