Select to view content in your preferred language

ESRI TOC Control

2884
10
07-26-2011 06:59 AM
RobertBrodsky
Deactivated User
Hi All;
I am using the TOC control in my application and need to chance which layer is active or inactive programmatically. Any ideas?

Thank you in advance.
0 Kudos
10 Replies
JenniferNery
Esri Regular Contributor
What do you mean by which layer is active? Do you mean visible or which layer received a mouse event? Layer.Visible property might be what you are looking for: http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Layer~Visi....

What TOC control are you using? Do you mean legend control? http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#LegendSimple
0 Kudos
AnastasiaAourik
Emerging Contributor
Is there a way to use the legendcontrol that nicely synchronizes with map layers visibility
BUT omit the symbology....
Thus, making it a simple list of layers with the checkbox?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
You can remove the swatches of the legend just by setting LegendItemTemplate to null (i.e in XAML LegendItemTemplate="{x:Null}").

Then you can change LayerItemTemplate and MapLayerItemTemplate to get checkboxes as in this sample : http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#LegendWithTemplates
0 Kudos
AnastasiaAourik
Emerging Contributor
Thank You so VERY MUCH....
That really helped get me started.

A couple of funky things...

I.
In xaml I specify LayerIDs="wellsgeneral,bitatg,completions,danddm,dtandr"
And the VS2010 intellisense give me message:
Unable to cast object of type 'Microsoft.Expression,DesignModel,DocumentModel.DocumentPrimitiveNode to type
Microsoft.Expression.DesignModel,DocumentModel.DocumentCompositeNode'.

The sample you specified lists layer ids this way. What can I do to fix this?

II.
In my scenario, I have many in each map layer
some features that are 'labels and have the name Label in it
(poor map design that I cannot change right now)
So I have to skip those layers and am using the code behind logic
Legend_refreshed where 'states' was removed (in the online sample)
I changed it to
           if (layerItemVM.Label.ToUpper().Contains("LABEL"))
                        removeLayerItemVM = layerItemVM;

Is this correct?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Unable to cast object of type 'Microsoft.Expression,DesignModel,DocumentModel.DocumentPrimitiveNode to type
Microsoft.Expression.DesignModel,DocumentModel.DocumentCompositeNode'.

The sample you specified lists layer ids this way. What can I do to fix this?


It is a known design issue but you can compile an run though.


I changed it to
if (layerItemVM.Label.ToUpper().Contains("LABEL"))
removeLayerItemVM = layerItemVM;

Is this correct?


Looks good 🙂
Except if you can have more than one layer to delete by service. In this case you need to store the items to delete in a list:
if (e.LayerItem.LayerItems != null)
{
  var toDelete = e.LayerItem.LayerItems.Where(item => item.Label.ToUpper().Contains("LABEL")).ToList();
  foreach (var item in toDelete)
    e.LayerItem.LayerItems.Remove(item);
}
0 Kudos
AnastasiaAourik
Emerging Contributor
Thank you so much...

I actually loaded the ids in the code behnd, after all map layers completed initializing...

            string[] layerIDs = { "wellsgeneral", "bitatg", "completion", "onetrax", "dandm", "dtandr" };
            this.googWellLayerList.LayerIDs = layerIDs;
            this.googWellLayerList.Refreshed += new EventHandler<Legend.RefreshedEventArgs>(googWellLayerList_Refreshed);
            this.googWellLayerList.Refresh();

Now, my only issue was that CheckBox_Clicked event that I had in my xaml on the Legend.LayerTemplate (see event)
                                    <esri:Legend.LayerTemplate>
                                        <DataTemplate>
                                            <CheckBox Content="{Binding Label}" IsChecked="{Binding IsEnabled, Mode=TwoWay}" Checked="CheckBox_Checked" />
                                        </DataTemplate>
                                    </esri:Legend.LayerTemplate>

KEPT getting fired before initiatialization was and the above googWellLayerList.Refresh() was arrived at.  So I just added a global bool to my application islegendrefresh = false, and I set it to true
after I finish the refreshed event logic.

In my checkbox_click event I had to cancel out if !islegendrefresh to prevent checkbox action occurring before I'm ready...

ANY WAY, YOU HAVE HELPED ALOT!
I was struggling with implementing this using my own listbox and keeping everyththing in sync with
visiblelayerslist via a custom LayerInfo : INotifyPropertyChanged...
I have alot to learn about advanced binding and notifications...

This Legend automatically does this for me.
0 Kudos
AnastasiaAourik
Emerging Contributor
BTW, I was hoping you can share information on the best way to change the name of the Label
that is bound to the checkbox content...
(see my xaml code)
<esri:Legend.LayerTemplate>                    
  <DataTemplate>                                         
    <CheckBox Content="{Binding Label}" IsChecked="{Binding IsEnabled, Mode=TwoWay}" Checked="CheckBox_Checked" />                                   
  </DataTemplate>                                 
</esri:Legend.LayerTemplate>
(see attached legend - the names are the internal map services names) - I'd like to give them some nice display names without messing with the binding behavior....
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Glad that helped you:)

A few more infos:

string[] layerIDs = { "wellsgeneral", "bitatg", "completion", "onetrax", "dandm", "dtandr" };
this.googWellLayerList.LayerIDs = layerIDs;
this.googWellLayerList.Refreshed += new EventHandler<Legend.RefreshedEventArgs>(googWellLayerList_Refreshed);
this.googWellLayerList.Refresh();



Refresh() is not useful, it will be done automatically when you set the LayerIDS.

<CheckBox Content="{Binding Label}" IsChecked="{Binding IsEnabled, Mode=TwoWay}" Checked="CheckBox_Checked" />


Not sure what you do on event CheckBox_Checked but the binding should be enough to manage the layers on/off.


I'd like to give them some nice display names without messing with the binding behavior....

You can use a binding with a converter or set the labels by code in event refreshed.
0 Kudos
AnastasiaAourik
Emerging Contributor
Thanks, I got that Value Converter now working beautifully.

On my checkbox, I have some extra things to do.

I have another layer (A feature layer of the same item as the map service layer that I have an ONDEMAND MAPTIP that I need to always turn on/off when layer is turned on off..

I now have all this working correctly, very nice.
===========================================

On another subject (probably should open a separate thread) I have issues with the
featurelayer in xaml it is hard to specify symbology, especially if it uses a complex expression based renderer - muche easier to use the symbology from the map service, but then I want to have the on demand map tips, so I have found it easier to have the map services layer using symbology from the map service, and a feature layer (pointing to the same map services layer id
with a null symbology but with the on demand map tip.
Do you know if I am mistaken in my thinking on this???

The other thing, I'd like to also bind Layer.Description or Description (see my xaml)
<esri:Legend.MapLayerTemplate>
  <DataTemplate>
     <StackPanel Orientation="Vertical" ToolTipService.ToolTip="{Binding Layer.Description, Mode=OneWay}">
        <sdk:Label Content="{Binding Label, Converter={StaticResource MapSvcNameConverter}}" />
        <StackPanel Orientation="Horizontal">
           <sdk:Label Content="Layer Transparency" Margin="2,0,4,0" FontSize="9" FontWeight="Light" Foreground="DarkGray"/>                                        
           <Slider Maximum="1" Value="{Binding Layer.Opacity, Mode=TwoWay}" Width="100" />
        </StackPanel>
     </StackPanel>
  </DataTemplate>
</esri:Legend.MapLayerTemplate>

None of these work.  In debugging, I also notice that Description is never populated.
I see it in the arcgis/rest/services reference but this does not get populated..
Note: I was debugging the legend refresh event handler and perusing the e.LayerItem
to try and find Description with my value....
Do you know if this is available via the Object Model?
0 Kudos