title: How do I bind Dictionary Value based on Key to a TextBlock in Code-Behind?
body: <p>I am using the MVVM model with a dynamic field generator, where the field is pulled from the database, done this way because different types of forms require different fields (TextBox/TextBlock, ComboBox, etc.). The problem is I'm trying to retrieve a value from a dictionary, to display in a TextBlock for the form, but I'm not sure how to bind the retrieved Key so I can display the value.</p>
  
  <p>Currently, I am doing the following:</p>
  
  <pre><code> TextBlock textBlock = new TextBlock();
   textBlock.SetBinding(TextBlock.TextProperty, createFieldBinding(myPropertyName);
  </code></pre>
  
  <p>With the following binding method:</p>
  
  <pre><code> private Binding createFieldBinding(string fieldName) {
        Binding binding = new Binding(fieldName);
        binding.Source = this.DataContext;
        binding.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
        return binding;
   }
  </code></pre>
  
  <p>Where I pass something through like <code>Score</code>, which maps to a <code>Score</code> property in the ViewModel, but how would I bind to a Dictionary Key to retrieve its Value?</p>
  
  <p>I want to be able to bind to something like <code>myDictionaryProperty[myDictionaryKey]</code>, if that is possible.</p>
  
  <p>Example:
  The below generates the <code>PlayerScore</code> for Player with ID of 1.
  Where <code>PlayerScore</code> is a <code>Dictionary&lt;int, int&gt;</code> and <code>PlayerID</code> is an <code>int</code>.</p>
  
  <pre><code> &lt;TextBlock Name="textBlockA" Text="{Binding PlayerScore[1]} /&gt;
  </code></pre>