Hi Kirk, I tried to debug your code:
<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>
and, as Jennifer mentionned, I confirm that the TargetName can't be resolved inside a datatemplate (and it's not a legend issue nor an ESRI issue). The TargetObject of the action is always null. In complement to the solution by code given by Jennifer, you might also create your own action which would initialize the Url and the map. Example of an action deriving fom SpatialQueryAction :public class LegendSpatialQueryAction : SpatialQueryAction
{
protected override void Invoke(object parameter)
{
var frameWorkElement = AssociatedObject as FrameworkElement;
var layerItem = (frameWorkElement == null ? null : frameWorkElement.DataContext as LayerItemViewModel);
var dynamicLayer = (layerItem == null ? null : layerItem.Layer as ArcGISDynamicMapServiceLayer);
var map = TargetObject as Map;
TargetObject = null; // reset in order to send OnTargetChanged
if (dynamicLayer != null && map != null)
{
if (layerItem.LayerType.Contains("MapLayer")) { } // TODO : decide what to do when action is called on map service layer item
Url = dynamicLayer.Url + "/" + layerItem.SubLayerID;
TargetObject = map;
base.Invoke(parameter);
}
}
}
and you can use it in Layer template this way:<esri:Legend.LayerTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Content="{Binding Label}"
IsChecked="{Binding IsEnabled, Mode=TwoWay}"
IsEnabled="{Binding IsInScaleRange}" VerticalAlignment="Center">
</CheckBox>
<Button Content="Select" Click="Button_Click" Margin="5,0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click" >
<local:LegendSpatialQueryAction DrawMode="Rectangle" LayerID="MyGraphicsLayer"
TargetObject="{Binding ElementName=MyMap}"
Symbol="{StaticResource GraphicsLayerFillSymbol}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
</DataTemplate>
</esri:Legend.LayerTemplate>
Note that the action should also initialize the symbol depending on the geometry type of the layer, but this is not done in my sample.