user857521
user857521

Reputation:

Syntax for adding items to Observablecollection

I have a Public Dictionary defined as

public Dictionary<string, double> comboBoxSelections { get; set; }

and add items to it using

this.comboBoxSelections = new Dictionary<string, double>();
comboBoxSelections.Add(currItem, d);

I want to use this as an ItemsSource for a ComboBox in WPF so whenever comboBoxSelections is updated I see the updated items available for selection in the ComboBox. So I think perhaps I need to wrap this in an ObservableCollection as

public ObservableCollection<Dictionary<string, double>> comboBoxSelections { get; set; }
this.comboBoxSelections = new ObservableCollection<Dictionary<string, double>>();

What is the syntax for adding new Dictionary items to the ObservableCollection? Something like...

comboBoxSelections.Add(new Dictionary<string, double>(currItem, d));

Upvotes: 0

Views: 933

Answers (1)

Ivan Leonenko
Ivan Leonenko

Reputation: 2383

You can use ObservableCollection<KeyValuePair<string,double>> or ObservableCollection<Tuple<string,double>> then you can bind to these values in XAML.

Or you can use ObservableDictionary check here and there.

Upvotes: 3

Related Questions