Select to view content in your preferred language

Toggle visibility of graphic in GraphicsLayer

4469
7
Jump to solution
03-09-2012 05:17 AM
KiaPierce
Emerging Contributor
Is there a way to toggle the visibility of a graphic in a GraphicsLayer?
0 Kudos
1 Solution

Accepted Solutions
deleted-user-ATjHIWsdQYmT
Deactivated User
Thanks Andrew.  However, there are a few cases in our app where we would like to toggle the visibility of an individual graphic within a GraphicsLayer and not the visiblity of the entire GraphicsLayer.  Is there a way to do this?


What you could do is loop through all of the graphics in your graphics layer and when you find the one you want to toggle (based on a specific attribute for instance) you could change it's symbology to transparent.  Then to toggle it back on, iterate through your graphics in your Graphics Layer again, and change it's symbology back.

for each gr as graphic in trycast(map.layers("MyGraphicsLayer"),graphicslayer)     if gr.Attributes("NAME") = "TargetGraphic" Then         if checkbox.isChecked = true then            gr.symbol = CType(LayoutRoot.Resources("TransparentSymbol"),ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol)        else           gr.symbol = CType(LayoutRoot.Resources("DefaultSymbol"),ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol)        end if      end if next

View solution in original post

0 Kudos
7 Replies
deleted-user-ATjHIWsdQYmT
Deactivated User
GraphicsLayer has a visible property.  You could bind the visibility to a checkbox or whatever control you want.
http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.GraphicsLa...

0 Kudos
KiaPierce
Emerging Contributor
Thanks Andrew.  However, there are a few cases in our app where we would like to toggle the visibility of an individual graphic within a GraphicsLayer and not the visiblity of the entire GraphicsLayer.  Is there a way to do this?
0 Kudos
deleted-user-ATjHIWsdQYmT
Deactivated User
Thanks Andrew.  However, there are a few cases in our app where we would like to toggle the visibility of an individual graphic within a GraphicsLayer and not the visiblity of the entire GraphicsLayer.  Is there a way to do this?


What you could do is loop through all of the graphics in your graphics layer and when you find the one you want to toggle (based on a specific attribute for instance) you could change it's symbology to transparent.  Then to toggle it back on, iterate through your graphics in your Graphics Layer again, and change it's symbology back.

for each gr as graphic in trycast(map.layers("MyGraphicsLayer"),graphicslayer)     if gr.Attributes("NAME") = "TargetGraphic" Then         if checkbox.isChecked = true then            gr.symbol = CType(LayoutRoot.Resources("TransparentSymbol"),ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol)        else           gr.symbol = CType(LayoutRoot.Resources("DefaultSymbol"),ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol)        end if      end if next
0 Kudos
LanceCrumbliss
Frequent Contributor

I could be wrong but if a GraphicsLayer is using a Renderer, then an individual graphic's symbol property will be null (or maybe it has changed or even "fixed" if that wasn't ESRI's original intention) so the method you suggest may not always be applicable.

In any case, I found this on another post somewhere and it works great.  Basically, setting the geometry to null hides the symbol.  This also doesn't require any reflection; it's pure OO inheritance.

Public Class ExtendedGraphic

    Inherits ESRI.ArcGIS.Client.Graphic

    Private _storedGeometry As ESRI.ArcGIS.Client.Geometry.Geometry

    Public Property Visible() As Boolean

        Get

            Return Me.Geometry IsNot Nothing

        End Get

        Set(ByVal value As Boolean)

            If value <> Visible Then

                If value Then

                    If _storedGeometry IsNot Nothing Then Me.Geometry = _storedGeometry

                Else

                    If Me.Geometry IsNot Nothing Then _storedGeometry = Me.Geometry

                    Me.Geometry = Nothing

                End If

            End If

        End Set

    End Property

End Class

0 Kudos
KiaPierce
Emerging Contributor
Awesome. Thanks Andrew
0 Kudos
MaximMikhisor
Deactivated User

Type typeInQuestion = typeof(Graphic);

PropertyInfo field = typeInQuestion.GetProperty("Visibility", BindingFlags.NonPublic | BindingFlags.Instance);

field.SetValue(graphic, Visibility.Collapsed);

0 Kudos
dotMorten_esri
Esri Notable Contributor

@Maxim: Note that changing internal properties through reflection can cause a lot of problems - You're really pulling the rug out under the SDK. Also not that these internals might not be available in future updates.

0 Kudos