Opacity Slider

1030
7
02-09-2011 06:24 PM
adamestrada
New Contributor
There is an example opacity slider example in the gallery. It's pretty darn simple.

http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#LayerList

Does anyone know of an example of how to do this in the code behind?

Thanks,
Adam
0 Kudos
7 Replies
DominiqueBroux
Esri Frequent Contributor
Which part of the sample do you want to write in code behind?

The datatemplate part is not that obvious (but luckily not that often needed). To create a datatemplate with SL code you need the xamlreader. There is a sample in this thread : http://forums.arcgis.com/threads/18827-Convert-a-string-to-a-link-in-a-query-result?highlight=xamlre...
0 Kudos
adamestrada
New Contributor
Well this is what I have so far...No templates...

            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"  Width="200" Background="Transparent" Margin="10,10,10,10">
                <Grid>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch"  Background="Transparent"  Margin="10,0,0,0">
                            <CheckBox x:Name="WeatherLayer" IsChecked="{Binding Visible, Mode=TwoWay}"  Foreground="White"
                         Click="WmsLayer_Initialized" />
                            <!--Opacity slider-->
                            <Slider Margin="-5,0,0,0" Minimum="0" Maximum="1" Width="50" 
                                Value="{Binding Opacity, Mode=TwoWay}" Height="18" />
                            <TextBlock Text=" National Weather"  Foreground="White"/>
                    </StackPanel>
                </Grid>
            </StackPanel>


Which is pretty much straight from the example. My method WMSLayer_Initialized turns the weather layer on and off and now I would like to change it's opacity using a slider. Actually, I need to do it for layers in several stackpanels so what do you suggest I do?

String[] VisibleWmsLayers = { "nexrad-n0r-900913-m05m" };
                nexradWmsLayer.Layers = VisibleWmsLayers;
                nexradWmsLayer.Opacity = Slider.?


Thanks in advance for these otherwise novice questions.
Adam
0 Kudos
DominiqueBroux
Esri Frequent Contributor
 
<CheckBox x:Name="WeatherLayer" IsChecked="{Binding Visible, Mode=TwoWay}"  Foreground="White"
                         Click="WmsLayer_Initialized" />
 


My method WMSLayer_Initialized turns the weather layer on and off

Looks like you are doing the same thing twice, the binding to Visible is already supposed to turn on/off your layer.

<Slider Margin="-5,0,0,0" Minimum="0" Maximum="1" Width="50" 
                                Value="{Binding Opacity, Mode=TwoWay}" Height="18" />


nexradWmsLayer.Opacity = Slider.?

Same here. You are trying to duplicate by code what the binding is supposed to do.

I am not sure what you need in your application, but I would suggest you to start with something close to the sample and then figure out how to add the missing functionalities (for the moment I don't understand which one you need).
0 Kudos
adamestrada
New Contributor
Ahhh....the whole reason I am using x:Name="WMSLayer_Initialized" is because I would like to set the WMS URL in a config file rather than in the xaml (In case the client wants to change it without calling us). This method reads from this config file and displays the WMS in the viewer and a simple if statement turns the layer on and off with the opacity defined in the esri:WMSLayer tag.

        <esri:WmsLayer ID="Nexrad" Opacity="0.5"
                          SkipGetCapabilities="True" Initialized="WmsLayer_Initialized"  Visible="False" />


Now I want to add the Opacity Slider but it looks like you can't mix and match the bindings to what is going on in the code behind. Is that true? I am essentially calliing the same method twice in my xaml too, does that matter? Everything works except for the slider part of it. My inquiring mind wants to know...

Adam
0 Kudos
DominiqueBroux
Esri Frequent Contributor

Now I want to add the Opacity Slider but it looks like you can't mix and match the bindings to what is going on in the code behind. Is that true? I am essentially calliing the same method twice in my xaml too, does that matter?

You can mix.... but I would not recommend this kind of mixture:).

Everything works except for the slider part of it. My inquiring mind wants to know...

So, for your mind only (but please don't use it:rolleyes:)
MySlider.SetBinding(Slider.ValueProperty, new Binding("Opacity") { Mode = BindingMode.TwoWay, Source = nexradWmsLayer });
0 Kudos
adamestrada
New Contributor
Does not like that...here is the method I am calling.

        private void WmsLayer_Initialized(object sender, EventArgs e)
        {
            try
            {
                WmsLayer nexradWmsLayer = null;
                nexradWmsLayer = (WmsLayer)Map.Layers["Nexrad"];
                String url = Configuration.getAGSAppSetting("weather");
                nexradWmsLayer.Url = url.ToString();
                String version = Configuration.getAGSAppSetting("weather_version");
                nexradWmsLayer.Version = version.ToString();
                //ToDo...Add layer list to config file...
                String[] VisibleWmsLayers = { "nexrad-n0r-900913-m05m" };
                nexradWmsLayer.Layers = VisibleWmsLayers;
                //this.WeatherSlider.SetBinding(Slider.ValueProperty, new Binding("Opacity"){ Mode = BindingMode.TwoWay, Source = nexradWmsLayer });
                bool visible = nexradWmsLayer.Visible;
                if (this.WeatherLayer.IsChecked == true)
                {
                    nexradWmsLayer.Visible = true;
                }
                if (this.WeatherLayer.IsChecked == false)
                {
                    nexradWmsLayer.Visible = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


You've already seen my xaml code. What am I doing wrong here? Can you tell that I am completely new to Silverlight?

Adam
0 Kudos
DominiqueBroux
Esri Frequent Contributor
At first glance, your code shoudl work after commenting out this line:

//this.WeatherSlider.SetBinding(Slider.ValueProperty, new Binding("Opacity"){ Mode = BindingMode.TwoWay, Source = nexradWmsLayer });


Anyway, it's not my recommended approach.
My proposal:
1) Keep the layerlist sample as it is --> you get the layers checkboxes and sliders working without any code.

2) Then you have to add your code to initialize the url of your wms layer. You can keep this code tied to the checkbox or to the Initialized event but this doesn't seem the best approach (no reason to reinitialize the url each time the user clicks on the checkbox).
Looks better to use an event which happens only once. Let's say : Map_Loaded.
As a side note, it's better to create a new wms layer instead of just changing the url better I am afraid that setting a new url to an existing layer is not always well managed by the wms layer.

So in xaml, add a loaded event to your map and remove the wms layer. Then in C# add the following code:
 
private void MyMap_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
    var map = sender as ESRI.ArcGIS.Client.Map;
    String url = Configuration.getAGSAppSetting("weather");
    String version = Configuration.getAGSAppSetting("weather_version");
    //ToDo...Add layer list to config file...
    String[] VisibleWmsLayers = { "nexrad-n0r-900913-m05m" };
 
    map.Layers.Add(new WmsLayer() { 
        Url = url,
        Version = version,
        Layers = VisibleWmsLayers ,
        ID="Nexrad"
    });
}


Hope this help.
0 Kudos