---
title: "Dictionary Failure binding in WPF"  
description: "Dictionary Failure binding in WPF"  
author: "Anonymous User"  
published: 2014-04-10  
updated: 2014-04-11  
canonical: https://www.mindstick.com/forum/2010/dictionary-failure-binding-in-wpf  
category: "wpf"  
tags: ["wpf"]  
reading_time: 3 minutes  

---

# Dictionary Failure binding in WPF

I have most of the textblock and [radio button](https://www.mindstick.com/articles/12743/radio-button-in-android) objects in my app bound to my dictionnary

I store the new value in a [Dictionary](https://www.mindstick.com/articles/1500/hashtable-and-dictionary-in-c-sharp).

The problem is that the binding failure is throwing an exception that is very slow.

Here is an example: (this code runs correctly, it is just slow if it can't find a match in the start app.)

```
<StackPanel>    <TextBox Text="{Binding Dictio[0], Mode=TwoWay}"/>    <TextBox Text="{Binding Dictio[1], Mode=TwoWay}"/>    <RadioButton GroupName="select" IsChecked="{Binding Dictio[2], Mode=TwoWay}" Content="true"/></stackPanel>
```

And in my class

```
    private Dictionary<int, object> _Dictio; public MainWindow()        {            Dictio = new Dictionary<int, object>();            this.DataContext = this;            InitializeComponent();        }public Dictionary<int, object> Dictio        {            get { return _Dictio; }            set { _Dictio = value; }        }private void test(object sender, RoutedEventArgs e){    foreach (KeyValuePair<int, object> kvp in Dictio)            {                Console.Out.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);        }}
```

When I try to start app and the binding tries to lookup Dictio[key], I get this in my [output window](https://www.mindstick.com/forum/160484/error-failed-to-launch-debug-adapter-additional-information-may-be-available-in-the-output-window) for all the key of my dictionnary Dictio:

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](https://www.mindstick.com/blog/233/get-checked-radiobutton-using-javascript)' (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. ---> 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](https://answers.mindstick.com/qa/92535/what-are-the-different-types-of-arguments), Signature sig, Boolean constructor)

System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] [parameters](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp), 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)'

Is there a way to make the [connection](https://www.mindstick.com/articles/13012/what-makes-ethernet-connection-better-than-wifi) not [throw an exception](https://www.mindstick.com/forum/159239/what-is-the-use-of-the-throw-keyword-to-manually-throw-an-exception-in-java) if the dictionary is empty?

## Replies

### Reply by Pravesh Singh

Hi Ankita,\

I use your class CustomDictionary

This is my xaml file

```
<StackPanel>    <TextBox Name="textBox1" Text="{Binding Dictio[0], Mode=TwoWay}"/>    <TextBox Name="textBox2" Text="{Binding Dictio[1], Mode=TwoWay}"/>    <RadioButton Name="radioButton" GroupName="select" IsChecked="{Binding Dictio[2], Mode=TwoWay}" Content="true"/><Button Content="test" Click="test"/></StackPanel>In my class    private CustomDictionary _Dictio; public MainWindow()        {            Dictio = new CustomDictionary();            this.DataContext = this;            InitializeComponent();        }    public CustomDictionary Dictio            {                get { return _Dictio; }                set { _Dictio = value; }            }    private void test(object sender, RoutedEventArgs e)    {        foreach (KeyValuePair<int, object> kvp in Dictio)                {                    Console.Out.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);            }    }
```

when i execute i have this error

Une [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) de première chance de type 'System.Collections.Generic.KeyNotFoundException' s'est produite dans mscorlib.dll

in your class CustomDictionnary in

return properties[index].Item1.GetValue(properties[index].Item2);


---

Original Source: https://www.mindstick.com/forum/2010/dictionary-failure-binding-in-wpf

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
