I ended up building the listbox in the cod behind as the layers in our map are added at startup. I used Jennifer's method of binding to the layer's Id instead of index and it works great. There's probably a better way to set the margins and width of the controls, but it'll work for now. XAML
<Grid x:Name="LayoutRoot" Style="{StaticResource LayoutRootGridStyle}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<esri:Map x:Name="map" Grid.ColumnSpan="4" Grid.Row="1" IsLogoVisible="False" >
<esri:Map.Extent>
<esri:Envelope XMin="-130" YMin="10" XMax="-70" YMax="60" >
<esri:Envelope.SpatialReference>
<esri:SpatialReference WKID="4326"/>
</esri:Envelope.SpatialReference>
</esri:Envelope>
</esri:Map.Extent>
<esri:Map.Layers>
<!--layers added at runtime -->
</esri:Map.Layers>
</esri:Map>
<!--TOC-->
<Border Background="#996495ED" BorderThickness="1" CornerRadius="5"
HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="20" Padding="5" BorderBrush="Black" Grid.Row="1" Grid.Column="0" >
<ListBox x:Name="MyList" ></ListBox>
</Border>
</Grid>
CS
void MapServicesLoadComplete(LoadOperation<Web.ApplicationMapServices> lo)
{
//Executes when the asynch domain service query completes; adds MapServices to the map
Layer layer;
ArcGISDynamicMapServiceLayer DynLayer = new ArcGISDynamicMapServiceLayer();
ArcGISTiledMapServiceLayer TileLayer = new ArcGISTiledMapServiceLayer();
foreach (var mapservice in lo.Entities)
{
if (mapservice.Cached == 1)
{
TileLayer = new ArcGISTiledMapServiceLayer();
TileLayer.Url = mapservice.ServerURL + "/" + mapservice.MapServiceName + "/MapServer";
TileLayer.ID = mapservice.MapServiceName;
layer = TileLayer;
}
else
{
DynLayer = new ArcGISDynamicMapServiceLayer();
DynLayer.Url = "http://" + mapservice.AGSServerName + "/arcgis/rest/services/" + mapservice.MapServiceName + "/MapServer";
DynLayer.ID = mapservice.MapServiceName;
layer = DynLayer;
}
layer.Opacity = mapservice.Transparancy == 0 ? 100 : 1- (Convert.ToDouble(mapservice.Transparancy)/100);
layer.Visible = Convert.ToBoolean(mapservice.Active);
map.Layers.Add(layer);
MyList.Items.Insert(0, BuildTOCitem(layer));
}
}
private StackPanel BuildTOCitem(Layer lyr)
{
StackPanel spNewItem = new StackPanel();
CheckBox cbx = new CheckBox();
Slider sldr = new Slider();
TextBlock tblock = new TextBlock();
spNewItem.Orientation = Orientation.Horizontal;
Binding bind = new Binding("Layers[" + lyr.ID + "].Visible");
bind.Mode = BindingMode.TwoWay;
bind.ElementName = "map";
cbx.SetBinding(CheckBox.IsCheckedProperty, bind);
bind = new Binding("Layers[" + lyr.ID + "].Opacity");
bind.Mode = BindingMode.TwoWay;
bind.ElementName = "map";
sldr.Margin = new Thickness(-5, 0, 0, 0);
sldr.Width = 50;
sldr.Minimum = 0;
sldr.Maximum = 1;
sldr.SetBinding(Slider.ValueProperty, bind);
bind = new Binding("Layers[" + lyr.ID + "].ID");
bind.Mode = BindingMode.OneWay;
bind.ElementName = "map";
tblock.Margin = new Thickness(5, 0, 0, 0);
tblock.SetBinding(TextBlock.TextProperty,bind);
spNewItem.Children.Add(cbx);
spNewItem.Children.Add(sldr);
spNewItem.Children.Add(tblock);
return spNewItem;
}