Populate a XAML combo box with field names from a dataset?

3047
11
09-13-2011 09:35 AM
JonathanHouck
New Contributor III
Hello everyone,

This is all using Silverlight 4 with the REST API and C# code behind.

I'm trying to populate a combobox with all the field names from a dataset so I can capture the user's selection and pass it into a query that they have constructed on their own.

I can't figure out how to get an array or list (or anything) with the field names that I can then iteratively add to the combobox.Items.  I've got some queries set up in my other project but they're querying against rows (features) and not just against the field names. I don't want to hard code any field names because I want the box to populate dynamically depending on which dataset they want to query.

Am I going about this wrong or just missing something obvious? Thanks for your help.

Jon
0 Kudos
11 Replies
JenniferNery
Esri Regular Contributor
If you are using FeatureLayer, you can use the following code to get Field.Names:
<ComboBox ItemsSource="{Binding ElementName=MyMap, Path=Layers[IncidentsLayer].LayerInfo.Fields}">
 <ComboBox.ItemTemplate>
  <DataTemplate>
   <TextBlock Text="{Binding Name}"/>
  </DataTemplate>
 </ComboBox.ItemTemplate>
</ComboBox>
0 Kudos
JonathanHouck
New Contributor III
Thank you for the answer! I will definitely try this.
0 Kudos
JonathanHouck
New Contributor III
I've got one followup question:

Is it possible to update that binding in the code behind?  I don't want to have to write a separate combobox for every single layer I want to query against; but when I try:

QueryFormCombo1.ItemsSource = "{Binding ElementName=MyMap, Path=Layers[MyLayer].LayerInfo.Fields}";


In the C# to change the binding it doesn't work; the combobox shows up empty.  Is there a way to do this? I'm worried I'm misunderstanding something about how feature layers work.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
You can't initialize a binding by code just by setting a string.

But anyway in your case, I am not sure you need a binding when done by code.
Just set the ItemsSource:
  QueryFormCombo1.ItemsSource = (myMap.Layers["MyLayer"] as FeatureLayer).LayerInfo.Fields;
0 Kudos
JonathanHouck
New Contributor III
Okay, everything in this thread has worked perfectly so far, but now in the next phase of this project I'm hitting another snag. 

When I populate the combobox this way, whenever I try to get the selected item as a string, it doesn't give me the actual field name, it gives me the type of object.

So If I go:

QueryFormCombo1.ItemsSource = (MyMap.Layers[LayerComboBoxItem] as FeatureLayer).LayerInfo.Fields;


And then I select and item, and go:

queryPartOne = QueryFormCombo1.SelectedItem.ToString();


It returns "ESRI.ArcGIS.Client.Field" and I can't figure out how to get the actual text, not just what it is.  I'm sorry if this is a dumb question, I'm kind of learning as I go here and I've been spinning my wheels on this one for awhile.
0 Kudos
MichaelKohler
Occasional Contributor II
I'm just guessing cause I'm new at this too!

queryPartOne = (QueryFormCombo1.SelectedItem As Field).Name;
0 Kudos
JonathanHouck
New Contributor III
Wow, thanks! Perfect, exactly what I was looking for.  Love these forums.
0 Kudos
MichaelKohler
Occasional Contributor II
yes... without these fourms my project would be dead in the water! I've gotten so much help and am just trying to pay it back while trolling the forums to find an answer to my current headache!
0 Kudos
BrianLeroux
Occasional Contributor III
Hi All,

I am usingthe following code to try to populate a combo box as explaned in this thread. The combobox populates but all the entries are "ESRI.ArcGIS.Client.Field". I can not figure out how to populate the box with the field values. Any ideas..

QueryComboBox.ItemsSource = (MapApplication.Current.SelectedLayer as FeatureLayer).LayerInfo.Fields;
0 Kudos