Bonjour,

J'essaie de lier un dictionnaire de données à une datagrid.

Voici mon dictionnaire :
Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
        /// <summary> Liste des correspondances entre les varibale tirés des fichiers et leur nature </summary>
        private ObservableDictionary<string, Nature> _correspondancesNature = new ObservableDictionary<string, Nature>();
 
        /// <summary>
        /// Liste des correspondances entre les varibale tirés des fichiers et leur nature
        /// </summary>
        public ObservableDictionary<string, Nature> CorrespondancesNature
        {
            get { return _correspondancesNature; }
            set { _correspondancesNature = value; }
        }

il est de type ObservableDictionary qui est définit comme ceci :
Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 
    public partial class ObservableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, INotifyCollectionChanged
    {
        public ObservableDictionary() : base() { }
        public ObservableDictionary(int capacity) : base(capacity) { }
        public ObservableDictionary(IEqualityComparer<TKey> comparer) : base(comparer) { }
        public ObservableDictionary(IDictionary<TKey, TValue> dictionary) : base(dictionary) { }
        public ObservableDictionary(int capacity, IEqualityComparer<TKey> comparer) : base(capacity, comparer) { }
        public ObservableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : base(dictionary, comparer) { }
 
        public event NotifyCollectionChangedEventHandler CollectionChanged;
 
        public new TValue this[TKey key]
        {
            get
            {
                return base[key];
            }
            set
            {
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, key, 0));
                base[key] = value;
            }
        }
 
        public new void Add(TKey key, TValue value)
        {
            base.Add(key, value);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, key, 0));
        }
 
        public new bool Remove(TKey key)
        {
            bool x = base.Remove(key);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, key, 0));
            return x;
        }
 
        public new void Clear()
        {
            base.Clear();
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }
 
 
        protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (CollectionChanged != null)
            {
                CollectionChanged(this, e);
            }
        }
    }

Et j'essaie de le lier à ma datagrid :
Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<DataGrid Grid.Row="1" IsEnabled="{Binding ElementName=_rdb_natureFromValue, Path=IsChecked}"
                                          Margin="2,0,0,0" ItemsSource="{Binding CorrespondancesNature}" 
                                          CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False"
                                          AutoGenerateColumns="False"><!--HeadersVisibility="None"-->
                                    <DataGrid.Columns>
                                        <DataGridTextColumn Header="Valeur" Binding="{Binding Key}" IsReadOnly="True"/>
                                        <DataGridTemplateColumn Header="Nature">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <ComboBox Width="120" ItemsSource="{Binding ItemsSource, ElementName=_cmb_lsNatures}"
                                                              SelectedItem="{Binding Value}">
                                                        <ComboBox.ItemTemplate>
                                                            <DataTemplate>
                                                                <TextBlock Text="{Binding NomNature}"/>
                                                            </DataTemplate>
                                                        </ComboBox.ItemTemplate>
                                                    </ComboBox>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                    </DataGrid.Columns>
                                </DataGrid>


Lors de l'exécution les erreurs suivantes apparaissent :
System.Windows.Data Error: 40 : BindingExpression path error: 'Key' property not found on 'object' ''String' (HashCode=-910855060)'. BindingExpressionath=Key; DataItem='String' (HashCode=-910855060); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'Value' property not found on 'object' ''String' (HashCode=-910855060)'. BindingExpressionath=Value; DataItem='String' (HashCode=-910855060); target element is 'ComboBox' (Name=''); target property is 'SelectedItem' (type 'Object')
les "string" en question ne sont autre que les clés de mon dictionnaire....

Le datacontext dans chaque ligne de mon datagrid sont donc les clé du dictionnaire alors que mon binding est fait sur le dictionnaire entier
Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
ItemsSource="{Binding CorrespondancesNature}"
et non pas sur les clés
Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
ItemsSource="{Binding CorrespondancesNature.Keys}"

J'ai vraiment l'impression que ceci n'a aucun sens....