Select to view content in your preferred language

Custom LegendItemTemplate

2360
3
10-15-2011 06:58 AM
SantoshV
Emerging Contributor
HI,
I am using a Legend template...
Here I want to show the polyline length which I query from the database
<esri:Legend Map="{Binding ElementName=map}" MaxHeight="300" MinHeight="100"
                         LayerItemsMode="Tree"  
                         ShowOnlyVisibleLayers="True" x:Name="CustomLegend" >

                    <esri:Legend.MapLayerTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <CheckBox Content="{Binding Label}"/>
                                <!--<Slider Maximum="1" Value="{Binding Layer.Opacity, Mode=TwoWay}" Width="50" />-->
                            </StackPanel>
                        </DataTemplate>
                    </esri:Legend.MapLayerTemplate>
                    <esri:Legend.LegendItemTemplate>
                        <DataTemplate >
                            <StackPanel Orientation="Horizontal" x:Name="stackpanel1">
                                <Image Source="{Binding ImageSource}"></Image>
                                <sdk:Label Content="{Binding Label}" MinWidth="40"/>
                                <sdk:Label Content="{Binding Keys}" MinWidth="50"/>
>
                            </StackPanel>
                        </DataTemplate>
                    </esri:Legend.LegendItemTemplate>
                    <esri:Legend.LayerTemplate>
                        <DataTemplate>
                            <CheckBox Content="{Binding Label}"
                           IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                        IsEnabled="{Binding IsInScaleRange}" >
                            </CheckBox>
                        </DataTemplate>
                    </esri:Legend.LayerTemplate>

                </esri:Legend>



how do I bind the values that I have in a Dictionary?
Please guide..
0 Kudos
3 Replies
DominiqueBroux
Esri Frequent Contributor
One way is you to use the LayerItemViewModel  'Tag' property to store your length.
You can initialize the 'Tag' property by code on event legend 'Refreshed' and then just display it with a Binding to Tag.

For more complex scenarios, you can also set the Tag property to a dictionary and use a binding such as {Binding Tag[Length]}   (by supposing your dictionary has a 'Length' entry)
0 Kudos
SantoshV
Emerging Contributor
One way is you to use the LayerItemViewModel  'Tag' property to store your length.
You can initialize the 'Tag' property by code on event legend 'Refreshed' and then just display it with a Binding to Tag.

For more complex scenarios, you can also set the Tag property to a dictionary and use a binding such as {Binding Tag[Length]}   (by supposing your dictionary has a 'Length' entry)


Hi thank you for your reply I tried doing it the way you suggested
void CustomLegend_Refreshed(object sender, ESRI.ArcGIS.Client.Toolkit.Legend.RefreshedEventArgs e)
        {
            if (boolRefreshLegend)
            {
                string[] str = new string[lstRoadSurfaceTypes.SelectedItems.Count];
                for (int i = 0; i < lstRoadSurfaceTypes.SelectedItems.Count; i++)
                {
                    KeyValuePair<int, string> ListItemKeyValuePair = (KeyValuePair<int, string>)lstRoadSurfaceTypes.SelectedItems;

                    str = ListItemKeyValuePair.Value;
                }

                e.LayerItem.Tag = "52 kms";

                var fLayer = e.LayerItem.Layer as FeatureLayer;
                if (fLayer != null && fLayer.ID == "Road Surface Type" && e.LayerItem.LegendItems != null)
                {
                    var toRemove = e.LayerItem.LegendItems.Where(item => item.Label != str[0]).ToArray();

                    int intLegendItemCount = e.LayerItem.LegendItems.Count;
                    for (int i = 0; i < str.Count(); i++)
                    {
                        toRemove = toRemove.Where(item => item.Label != str).ToArray();
                    }
                    LegendItemViewModel itemmodel = mainPage.CustomLegend.LayerItems[0];
                    itemmodel .Tag = "Black Top";

                    foreach (var item in toRemove)
                        e.LayerItem.LegendItems.Remove(item);
                }
            }
        }


<esri:Legend.LegendItemTemplate>
                        <DataTemplate >
                            <StackPanel Orientation="Horizontal" x:Name="stackpanel1">
                                <Image Source="{Binding ImageSource}"></Image>
                                <sdk:Label Content="{Binding Label}" MinWidth="40"/>
                                <sdk:Label x:Name="label1" Content="{Binding Tag}" MinWidth="40"/>
                            </StackPanel>
                        </DataTemplate>
                    </esri:Legend.LegendItemTemplate>


Please guide me here
0 Kudos
SantoshV
Emerging Contributor
One way is you to use the LayerItemViewModel  'Tag' property to store your length.
You can initialize the 'Tag' property by code on event legend 'Refreshed' and then just display it with a Binding to Tag.

For more complex scenarios, you can also set the Tag property to a dictionary and use a binding such as {Binding Tag[Length]}   (by supposing your dictionary has a 'Length' entry)



Thank you tag worked
<sdk:Label x:Name="label1" Content="{Binding Tag}" MinWidth="40"/>


            dicSurfaceLength = e.Result;
            foreach (LegendItemViewModel legendItems in mainPage.CustomLegend.LayerItems[0].LegendItems)
            {
                legendItems.Tag = e.Result[legendItems.Label];
            }
0 Kudos