5

I'd like to list attribute fields in a combobox. there are two combobox . cmblayers and cmbfields . cmblayers list map layers . user select a layer in cmblayer and attribute fields of selected layer list in cmbfield. i wrote cmblayer but i can't write cmbfield code.How can i write cmbfield using xaml or code behind? cmblayer code :

<ComboBox x:Name="cmblayers"  ItemsSource ="{Binding ElementName=Map, Path=Layers}"    >

                     <ComboBox.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding LayerInfo.Name}"/>
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>

</ComboBox>

i found the below code . but it just bind to specify layer. i couldn't bind cmblayers to cmbfields

<ComboBox x:Name="cmbFields" ItemsSource="{Binding ElementName=Map, Path=Layers[IncidentsLayer].LayerInfo.Fields}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
0

3 Answers 3

2
+25

The attribute fields for a map layer are not available in its LayerInfo. What you'll have to do is, when the cmblayers is selected, query the REST service for that map layer. When you get your layer data, then you can add the fields you requested to your cmbFields ItemSource. I'll have the code for this later.

2
  • I completely agree with you . what's your opinion about this code: cboFields.ItemsSource = (Map.Layers[cboLayers.SelectedIndex] as FeatureLayer).LayerInfo.Fields;
    – BBG_GIS
    Commented Nov 20, 2012 at 21:07
  • Looks like it should work.
    – raykendo
    Commented Nov 20, 2012 at 21:12
1

it's my answer : it's work properly .

first , layer combobox:

<ComboBox x:Name="cmblayers"  ItemsSource ="{Binding ElementName=Map, Path=Layers}"    >

                     <ComboBox.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding LayerInfo.Name}"/>
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>

</ComboBox> 

second, field combobox :

<ComboBox x:Name="cboFields"    ItemsSource="{Binding ElementName=Map}"  SelectionChanged="cboFields_SelectionChanged">

                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                <TextBlock Text="{Binding Name}"/>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>

finally, in cboLayers_SelectionChange write the below cod :

cboFields.ItemsSource = (Map.Layers[cboLayers.SelectedIndex] as FeatureLayer).LayerInfo.Fields;
            string queryPartOne = cboFields.SelectedItem.ToString();
            cboFields.Items.Add(queryPartOne);
4
  • You could improve your solution by binding the second combobox to the first which would remove the need to have code behind in the cboLayers_SelectionChange method. Commented Nov 21, 2012 at 9:02
  • How can i improve my solution without using code behin? could you please help me ?
    – BBG_GIS
    Commented Nov 21, 2012 at 12:31
  • 2
    You're basically trying to implement cascading comboboxes. Remove the cboLayers_SelectionChange code and instead bind the ItemsSource of the fields combobox to the selected item of the layers combobox. The list of fields should update automatically as you select a different layer without the need for code behind. This is the simplest example I can find of how to do it: stackoverflow.com/questions/1506810/…. You would also need to specify a value converter on the fields combobox to return a list of fields for the selected layer. Commented Nov 21, 2012 at 13:29
  • your solution works properly .
    – BBG_GIS
    Commented Nov 21, 2012 at 21:09
1

A more MVVM solution which I was trying to describe above (but probably failing)...

Bind the second combobox to the SelectedItem of the first combobox. Something like this:

<combobox x:Name="cmblayers" ItemsSource ="{Binding ElementName=Map, Path=Layers}"/>
<combobox x:Name="cboFields" ItemsSource="{Binding SelectedItem, ElementName=cmblayers, Converter={StaticResource LayerToFieldsConverter}}"/>

And define a value converter to take the selected layer and return the list of fields. Something like this:

public class LayerToFieldsConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // Code to return list of fields for the selected layer...
        FeatureLayer featureLayer = value as FeatureLayer;

        return featureLayer.LayerInfo.Fields;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

As you select a different layer the fields combobox should update without you needing to write any code behind. The converter class is just used to obtain a list of fields associated with the selected layer. It's only needed as the list of fields isn't a property of the layer itself and needs a bit of work to get it out.

I've not tested any of the above but should hopefully give you an idea of how to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.