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);
}
You can use:
List<object> values = odDict.Values.ToList();
cmbOverlayDistrict.Items.AddRange(items: values.ToArray());
Thank you for the reply but when using your code I now see this:
And if you use this?
List<object> values= new(odDict.Values);
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();
});
@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.