title: Dictionary Failure binding in WPF
body: <p>I have most of the textblock and radio button objects in my app bound to my dictionnary</p>
  
  <p>I store the new value in a Dictionary. </p>
  
  <p>The problem is that the binding failure is throwing an exception that is very slow.</p>
  
  <p>Here is an example: (this code runs correctly, it is just slow if it can't find a match in the start app.)</p>
  
  <pre><code>&lt;StackPanel&gt;
      &lt;TextBox Text="{Binding Dictio[0], Mode=TwoWay}"/&gt;
      &lt;TextBox Text="{Binding Dictio[1], Mode=TwoWay}"/&gt;
      &lt;RadioButton GroupName="select" IsChecked="{Binding Dictio[2], Mode=TwoWay}" Content="true"/&gt;
  &lt;/stackPanel&gt;
  </code></pre>
  
  <p>And in my class</p>
  
  <pre><code>    private Dictionary&lt;int, object&gt; _Dictio;
  
   public MainWindow()
          {
              Dictio = new Dictionary&lt;int, object&gt;();
              this.DataContext = this;
              InitializeComponent();
          }
  
  public Dictionary&lt;int, object&gt; Dictio
          {
              get { return _Dictio; }
              set { _Dictio = value; }
          }
  
  private void test(object sender, RoutedEventArgs e)
  {
      foreach (KeyValuePair&lt;int, object&gt; kvp in Dictio)
              {
                  Console.Out.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
          }
  }
  </code></pre>
  
  <p>When I try to start app and the binding tries to lookup Dictio[key], I get this in my output window for all the key of my dictionnary Dictio:</p>
  
  <pre><code> System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'Object') from 'Dictio' (type 'Dictionary`2'). BindingExpression:Path=Dictio[4]; DataItem='MainWindow' (Name=''); target element is 'RadioButton' (Name='mm'); target property is 'IsChecked' (type 'Nullable`1') TargetInvocationException:'System.Reflection.TargetInvocationException: Une exception a été levée par la cible d'un appel. ---&gt; System.Collections.Generic.KeyNotFoundException: La clé donnée était absente du dictionnaire.
     à System.Collections.Generic.Dictionary`2.get_Item(TKey key)
     --- Fin de la trace de la pile d'exception interne ---
     à System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
     à System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
     à System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
     à System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
     à MS.Internal.Data.PropertyPathWorker.GetValue(Object item, Int32 level)
     à MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)'
  </code></pre>
  
  <p>Is there a way to make the connection not throw an exception if the dictionary is empty</p>