Select to view content in your preferred language

Adding items to combobox list using a dictionary

382
6
04-03-2025 05:16 AM
mstranovsky
Occasional Contributor

I have a dictionary of values named "odDict".   When i use the following code it only adds the first value in the dictionary.  How can i get it to populate the list using all values in the dictionary?

foreach (KeyValuePair<string, string> kvp in odDict)
{
string key = kvp.Key;
string val = kvp.Value;
cmbOverlayDistrict.Items.Add(val);
}

0 Kudos
6 Replies
RadekMandovec
Occasional Contributor

You can use:

List<object> values = odDict.Values.ToList();
cmbOverlayDistrict.Items.AddRange(items: values.ToArray());
0 Kudos
mstranovsky
Occasional Contributor

Thank you for the reply but when using your code I now see this:

mstranovsky_0-1743688270209.png

mstranovsky_1-1743688287829.pngmstranovsky_2-1743688310143.png

 

 

0 Kudos
RadekMandovec
Occasional Contributor

And if you use this?

List<object> values= new(odDict.Values);

 

0 Kudos
SumitMishra_016
Frequent Contributor

I have put my sample code, it is working for me

<ComboBox Name="CaseNumberComboBox" Width="200" ItemsSource="{Binding CaseNumbers}" SelectionChanged="CaseNumberComboBox_SelectionChanged"/>
public ObservableCollection<string> CaseNumbers { get; set; } = new();


// Temporary list to avoid multiple UI updates
List<string> caseNumbersList = new();

while (await reader.ReadAsync())
{
    string caseNo = reader.GetString(0);
    caseNumbersList.Add(caseNo);
}

// Update UI on the main thread
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
    CaseNumbers.Clear();
    foreach (var caseNo in caseNumbersList)
    {
        CaseNumbers.Add(caseNo);
    }
    // Select the latest case number
    CaseNumberComboBox.SelectedItem = CaseNumbers.FirstOrDefault();
});
0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

@mstranovsky  Your code snippet is not applicable in an ArcGIS Pro Add-in.  There are two patterns in ArcGIS Pro to deploy a ComboBox:

1)   If you are using a Pro Combo box ribbon control you would load the 'Ribbon' Dropdown from a dictionary like this:

internal class TestComboBox : ComboBox
{
	private bool _isInitialized;

	/// <summary>
	/// Combo Box constructor
	/// </summary>
	public TestComboBox()
	{
		UpdateCombo();
	}

	// create a dictionary with an entry for each US state, the value should be the capital city of that state
	private Dictionary<string, string> _stateCapitals = new Dictionary<string, string>
	{
		{ "Alabama", "Montgomery" },
		{ "Alaska", "Juneau" },
		{ "Arizona", "Phoenix" },
		{ "Arkansas", "Little Rock" },
		{ "California", "Sacramento" },
		{ "Colorado", "Denver" },
		{ "Connecticut", "Hartford" },
		{ "Delaware", "Dover" },
		{ "Florida", "Tallahassee" },
		{ "Georgia", "Atlanta" }
	};

	/// <summary>
	/// Updates the combo box with all the capitals of the US states
	/// </summary>
	private void UpdateCombo()
	{
		if (_isInitialized)
			SelectedItem = ItemCollection.FirstOrDefault();
		if (!_isInitialized)
		{
			// Updates the combo box with all the capital names of the US states
			foreach (var capital in _stateCapitals.Values)
			{
				Add(new ComboBoxItem(capital));
			}
			_isInitialized = true;
		}
		Enabled = true;
		SelectedItem = ItemCollection.FirstOrDefault();
	}

	/// <summary>
	/// The on comboBox selection change event. 
	/// </summary>
	/// <param name="item">The newly selected combo box item</param>
	protected override void OnSelectionChange(ComboBoxItem item)
	{
		if (item == null) return;
		if (string.IsNullOrEmpty(item.Text)) return;
	}
}

2. If you are using a combobox on a Dockpane, you should use the MVVM pattern to populate the combobox content.  In this case, please model your code after @SumitMishra_016 's sample snippet.

Wolf
by Esri Regular Contributor
Esri Regular Contributor

I attached a small sample add-in project that has a Combo dropdown on the ArcGIS Pro ribbon and two dropdowns on a Dockpane.  I am using a dictionary (keys and values) to 'feed' the dropdown content.

Screenshot1.png

0 Kudos