2

I'm trying to use a button with behavior in the MapLayerTemplate of an Esri Silverlight SDK 2.1 legend.

When I click the button and then drag on the map, the map pans instead of showing a selection rectangle.

The behavior code is pulled from this sample, while the legend is from this sample.

If I put the same xaml code within the layoutroot, it works as expected - I see a selection rectangle and the selection completes successfully.

<esri:Legend x:Name="MyLegend" Map="{Binding ElementName=MyMap}"  
            LayerIDs="ProjectPolys,AssetPoints,AssetLines,BaseLayers,WaterInfrastructure,SewerInfrastructure"
            LayerItemsMode="Tree" 
            ShowOnlyVisibleLayers="False"
            Refreshed="Legend_Refreshed">
    <esri:Legend.MapLayerTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
            <CheckBox Content="{Binding Label}"
                    IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                    IsEnabled="{Binding IsInScaleRange}">                                
                </CheckBox>
                <Button Content="Select">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <esriBehaviors:SpatialQueryAction         
                                DrawMode="Rectangle"         
                                LayerID="MyGraphicsLayer"         
                                Url="http://hq-gis-01/ArcGIS/rest/services/WaterInfrastructure2/MapServer/15"
                                Symbol="{StaticResource GraphicsLayerFillSymbol}"         
                                TargetName="MyMap" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>
                <Slider Maximum="1" Value="{Binding Layer.Opacity, Mode=TwoWay}" Width="50" />
            </StackPanel>
        </DataTemplate>
    </esri:Legend.MapLayerTemplate>
    <esri:Legend.LayerTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding Label}"
            IsChecked="{Binding IsEnabled, Mode=TwoWay}"
            IsEnabled="{Binding IsInScaleRange}" >
            </CheckBox>
        </DataTemplate>
    </esri:Legend.LayerTemplate>
</esri:Legend>

Update: Following Seth's suggestion I tried wiring the trigger in the code-behind. I get the same results. The code behind approach fails for the button in the datatemplate, but works for the one on a stackpanel sibling to the map.

private void Assign_Behavior_Click(object sender, RoutedEventArgs e)
{
    var button = sender as Button;
    ESRI.ArcGIS.Client.Actions.SpatialQueryAction sqa = new ESRI.ArcGIS.Client.Actions.SpatialQueryAction();
    sqa.DrawMode = DrawMode.Rectangle;
    sqa.LayerID = "MyGraphicsLayer";
    sqa.Url = @"http://hq-gis-01/ArcGIS/rest/services/WaterInfrastructure2/MapServer/15";
    sqa.Symbol = LayoutRoot.Resources["GraphicsLayerFillSymbol"] as Symbol;
    sqa.TargetName = "MyMap";

    var et = new System.Windows.Interactivity.EventTrigger("Click");
    et.Actions.Add(sqa);
    var triggerColl = System.Windows.Interactivity.Interaction.GetTriggers(button);
    triggerColl.Add(et);
}

I'm beginning to think this is not an Esri issue - I can't find an example anywhere showing how to attach trigger actions from buttons in a datatemplate.

  • What happens when you click the button? Does it enter the Assign_Behavior_Click event and then do nothing? – Britt Wescott Dec 8 '10 at 21:03
1

I was able to replicate this issue, it may be a bug with ESRI's API. You can put a Click event in the Select button to hit the code-behind and then use this technique to call the action:

http://forums.arcgis.com/threads/3696-Measure-Action-in-Code?p=11969&viewfull=1#post11969

As Dominique states, it's probably not recommended to use Actions in the code-behind, however in this case, I don't see any alternative.

  • Thanks Seth. I've updated my question with your suggestion. – Kirk Kuykendall Dec 8 '10 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.