Controlling visibility of controls for a layer in the TOC

923
6
10-05-2011 05:39 AM
PaulHuppé
Occasional Contributor
Hi,

I have the following template for layers in the TOC:

                <esri:Legend.LayerTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <!--Busy indicator-->
                            <ContentControl ContentTemplate="{StaticResource BusyIndicatorTemplate}"
           Visibility="{Binding BusyIndicatorVisibility}"/>
                            <CheckBox
                                ToolTipService.ToolTip="{Binding localizationResources.strShowlayer}"
                 IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                 IsEnabled="{Binding IsInScaleRange}" >
                            </CheckBox>
                            <Image x:Name="imgZoomTo" Source="/MIB.Map.Presentation;component/Images/zoom.png"
                                   DataContext="{Binding ElementName=Toc,Path=ViewModel}"
                                   ToolTipService.ToolTip="Zoom"
                                   Tag="{Binding Content, ElementName=lblLayerName}"
                                   Cursor="Hand">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="MouseLeftButtonDown">
                                        <i:InvokeCommandAction Command="{Binding ZoomToCommand}"
                                                               CommandParameter="{Binding ElementName=imgZoomTo}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Image>
                            <RadioButton x:Name="rbLayer"
                                        DataContext="{Binding ElementName=Toc,Path=ViewModel}"
                                        IsChecked="False" 
                                        GroupName="grpSelectedLayer"
                                        Tag="{Binding Content, ElementName=lblLayerName}"
                                        ToolTipService.ToolTip="{Binding localizationResources.strSelectLayer}"
                                        Content="" >
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Checked">
                                        <i:InvokeCommandAction Command="{Binding RadioButtonCheckedCommand}"
                                                               CommandParameter="{Binding ElementName=rbLayer}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </RadioButton>
                            <sdk:Label x:Name="lblLayerName"
                                       Content="{Binding Label}">
                                <ToolTipService.ToolTip>
                                    <StackPanel MaxWidth="400">
                                        <TextBlock FontWeight="Bold" Text="{Binding Layer.ID}" TextWrapping="Wrap" />
                                        <TextBlock FontWeight="Bold" Text="{Binding Label}" TextWrapping="Wrap" />
                                        <TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
                                        <TextBlock Text="{Binding MinimumResolution, StringFormat='Minimum Resolution : {0:F6}'}" />
                                        <TextBlock Text="{Binding MaximumResolution, StringFormat='Maximum Resolution : {0:F6}'}" />
                                    </StackPanel>
                                </ToolTipService.ToolTip>
                            </sdk:Label>
                        </StackPanel>
                    </DataTemplate>
                </esri:Legend.LayerTemplate>


In this template, there is a RadioButton control called "rbLayer".  In code-behind, I would like to control the visibility of that control.  My question is: how do I get a hold of that control so that I can change its visibility?

Thanks,
Paul
0 Kudos
6 Replies
MichaelKohler
Occasional Contributor II
One way that I use for certain things is to set the Visibility="{Binding Tag}" for the radio button and change the layers tag to xxxx.Tag = Visibility.Collapsed or Visibility.Visible in the Code Behind.
0 Kudos
PaulHuppé
Occasional Contributor
Hi,

One way that I use for certain things is to set the Visibility="{Binding Tag}" for the radio button and change the layers tag to xxxx.Tag = Visibility.Collapsed or Visibility.Visible in the Code Behind.


Yes, I know how to change the visibility of a control, but I need to get to the control from code-behind as defined in the template.  For each service that I use, this template is applied to all layers inside the service.  Once the legend is loaded, I need to be able to access the controls in the TOC for each layer and manipulate the Visibility property.

I hope this clarified what I am trying to do.

Paul
0 Kudos
MichaelKohler
Occasional Contributor II
From what I understand... you can not specifically get that control in code behind. What I described is how I change the visiblility of the opacity slider for various layers in my legend control. For example, I don't need an opacity slider for a point layer. So I hide it using the method I described.

Add a handler to your legend control for Refreshed

esri:Legend Map="{Binding ElementName=MyMap}"  
                    LayerIDs="......."
                    LayerItemsMode="Tree" 
                    ShowOnlyVisibleLayers="False"
                    Refreshed="Legend_Refreshed"   
                    LayoutUpdated="Legend_LayoutUpdated"
                    Background="#FF1B1D1D"
                    Foreground="White" Opacity="0.75" Grid.Row="1" >


Then add the Visiblility in your radio button control
 <RadioButton x:Name="rbLayer"
                                        DataContext="{Binding ElementName=Toc,Path=ViewModel}"
                                        IsChecked="False" 
                                        GroupName="grpSelectedLayer"
                                        Tag="{Binding Content, ElementName=lblLayerName}"
                                        ToolTipService.ToolTip="{Binding localizationResources.strSelectLayer}"
                                        Visibility="{Biniding Tag}"
                                        Content="" >
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Checked">
                                        <i:InvokeCommandAction Command="{Binding RadioButtonCheckedCommand}"
                                                               CommandParameter="{Binding ElementName=rbLayer}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </RadioButton>




Then in your Legend_Refreshed void
if (e.LayerItem.Layer.ID == "SomeLayerName") e.LayerItem.Tag = Visibility.Collapsed;


then when the legend loads/is refreshed the radio button will bind to the tags value and appear or disappear.

You can also dynamically change the layers Tag property in code behind and refresh the legend to make it visible or invisible if that is what you want to do.

I got help from Dominique here: http://forums.arcgis.com/threads/34809-Legend-with-Templates-sample?p=117312#post117312
0 Kudos
BrandonCopeland
New Contributor
It seems that the visibility of your radio button is really just about UI, not the underlying model. 2 options taking that perspective...

Create Visual States to represent different visual representations of your interface (RadioButton visible or not) and change the state based on whatever logic applies. This could be done with in XAML with a DataStateBehavior.

If its only the visibility of a single control you're concerned about, that could be overkill. Alternatively, you could bind the visibility to whatever value is determining it and build an IValueConverter to return Visible or Collapsed based on that value.
0 Kudos
PaulHuppé
Occasional Contributor
Hi,

Thanks Micheal.  That is what I did and it works.  I have been away from my application development for about 4 months and I was already rusty!

Cheers,
Paul
0 Kudos
TanyaOwens
Occasional Contributor III
Hello,

I am trying to do something similar but I am completely stuck. Maybe if you would be willing to take a look at my forum post to see if you can point me in the right direction I would greatly appreciate it.

http://forums.arcgis.com/threads/66829-Radio-buttons-to-toggle-layers-from-a-Dynamic-map-service-lay...

Thanks!!
0 Kudos