+ Reply to Thread
Results 1 to 12 of 12

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

  1. #1
    Jonathan Houck
    Join Date
    Jan 2011
    Posts
    26
    Points
    6
    Answers Provided
    1


    0

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

    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

  2. #2
    Jennifer Nery

    Join Date
    May 2010
    Posts
    2,449
    Points
    289
    Answers Provided
    51


    0

    Default Re: Populate a XAML combo box with field names from a dataset?

    If you are using FeatureLayer, you can use the following code to get Field.Names:
    Code:
    <ComboBox ItemsSource="{Binding ElementName=MyMap, Path=Layers[IncidentsLayer].LayerInfo.Fields}">
    	<ComboBox.ItemTemplate>
    		<DataTemplate>
    			<TextBlock Text="{Binding Name}"/>
    		</DataTemplate>
    	</ComboBox.ItemTemplate>
    </ComboBox>
    Jennifer Nery
    Software Developer
    ArcGIS API for SL/WPF/WP7

  3. #3
    Jonathan Houck
    Join Date
    Jan 2011
    Posts
    26
    Points
    6
    Answers Provided
    1


    0

    Default Re: Populate a XAML combo box with field names from a dataset?

    Thank you for the answer! I will definitely try this.

  4. #4
    Jonathan Houck
    Join Date
    Jan 2011
    Posts
    26
    Points
    6
    Answers Provided
    1


    0

    Default Re: Populate a XAML combo box with field names from a dataset?

    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:

    Code:
    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.

  5. #5
    Dominique BROUX

    Join Date
    Jan 2010
    Posts
    2,409
    Points
    743
    Answers Provided
    130


    0

    Default Re: Populate a XAML combo box with field names from a dataset?

    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;
    /Dominique

  6. #6
    Jonathan Houck
    Join Date
    Jan 2011
    Posts
    26
    Points
    6
    Answers Provided
    1


    0

    Default Re: Populate a XAML combo box with field names from a dataset?

    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:

    Code:
    QueryFormCombo1.ItemsSource = (MyMap.Layers[LayerComboBoxItem] as FeatureLayer).LayerInfo.Fields;
    And then I select and item, and go:

    Code:
    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.

  7. #7
    Michael Kohler
    Join Date
    Nov 2010
    Posts
    53
    Points
    0
    Answers Provided
    0


    0

    Default Re: Populate a XAML combo box with field names from a dataset?

    I'm just guessing cause I'm new at this too!

    queryPartOne = (QueryFormCombo1.SelectedItem As Field).Name;

  8. #8
    Jonathan Houck
    Join Date
    Jan 2011
    Posts
    26
    Points
    6
    Answers Provided
    1


    0

    Default Re: Populate a XAML combo box with field names from a dataset?

    Wow, thanks! Perfect, exactly what I was looking for. Love these forums.

  9. #9
    Michael Kohler
    Join Date
    Nov 2010
    Posts
    53
    Points
    0
    Answers Provided
    0


    0

    Default Re: Populate a XAML combo box with field names from a dataset?

    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!

  10. #10
    Brian Leroux
    Join Date
    Aug 2010
    Posts
    231
    Points
    40
    Answers Provided
    12


    0

    Default Re: Populate a XAML combo box with field names from a dataset?

    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..

    Code:
    QueryComboBox.ItemsSource = (MapApplication.Current.SelectedLayer as FeatureLayer).LayerInfo.Fields;

  11. #11
    Lance Crumbliss
    Join Date
    Apr 2010
    Posts
    173
    Points
    31
    Answers Provided
    6


    0

    Default Re: Populate a XAML combo box with field names from a dataset?

    Quote Originally Posted by Bleroux View Post
    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..

    Code:
    QueryComboBox.ItemsSource = (MapApplication.Current.SelectedLayer as FeatureLayer).LayerInfo.Fields;
    What happens if you set the ComboBox's DisplayMemberPath to "Name"?

  12. #12
    Brian Leroux
    Join Date
    Aug 2010
    Posts
    231
    Points
    40
    Answers Provided
    12


    0

    Default Re: Populate a XAML combo box with field names from a dataset?

    Quote Originally Posted by chuckyboy81070 View Post
    What happens if you set the ComboBox's DisplayMemberPath to "Name"?
    Thanks for the suggestion. I will give it a try when I have a chance. I was able to populate my combo box with the following code.
    Code:
    private void updateQueryCombo()
            {
                foreach (Field field in (MapApplication.Current.SelectedLayer as FeatureLayer).LayerInfo.Fields)
                {
                    QueryComboBox.Items.Add(field.Name.ToString());
                }
            }

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts