How do I get the currently selected item in a ComboBox?

1522
3
08-29-2017 11:50 AM
Highlighted
Occasional Contributor

I'm having a hard time figuring out how to get the currently selected item in a ComboBox.  I couldn't find anything demonstrating this in the ArcGIS Pro SDK Community Samples, but I may have missed or overlooked this.  The RibbonControls solution demonstrates the combo box and button click, but I can't figure out how to get the combo box selection when the button is clicked.  Can anyone point me in the right direction?

Tags (4)
Reply
0 Kudos
3 Replies
Highlighted
by
New Contributor III

This is an example from my code I'm using, Itemsource is bound to PolyFeatureLayers, a public method used to get a private Collection that holds map Feature layers. Then selected item is bound to SelectedPolyFeatureLayer, a public method used to get a private FeatureLayer. This was actually adapted from one of the community samples.

This is an active binding, which means at the variables will be immediately updated to have the selected combobox items, without need for buttons to refresh them.

XAML

<ComboBox Grid.Column="1" ItemsSource="{Binding PolyFeatureLayers}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedPolyFeatureLayer}" />

C#

private FeatureLayer _selectedPolyFeatureLayer;
private ObservableCollection<FeatureLayer> _polyFeatureLayers = new ObservableCollection<FeatureLayer>();

public ReadOnlyObservableCollection<FeatureLayer> PolyFeatureLayers {
   get { return new ReadOnlyObservableCollection<FeatureLayer>(_polyFeatureLayers); }
}

public FeatureLayer SelectedPolyFeatureLayer {
   get { return _selectedPolyFeatureLayer; } //return selected feature
   set { _selectedPolyFeatureLayer = value; } //set selected feature (the combobox uses this), value is what is passed to it
}

Reply
0 Kudos
Highlighted
Occasional Contributor

Thanks for this!  Do you happen to remember which community sample demonstrates this?  I must just not be seeing it...

Reply
0 Kudos
Highlighted
by Esri Regular Contributor
Esri Regular Contributor

Hi Matthew,  the easiest way to find sample source code snippets like the one you are looking for is to use the [new and improved] GitHub search on the top of the repository main page.  Once you navigate to the GitHub repository arcgis-pro-sdk-community-samples look at the top left of the page an you will see "This repository ... Search".  Since comboboxes bind to the 'SelectedItem" property simply paste "SelectedItem="{Binding" into the search box and click enter.  The results will have links to all samples utilizing this code snippet.

Reply
0 Kudos