We are using ArcGIS Runtime .NET 10.2.6 and the legend-control in ArcGIS Runtime Toolkit for Windows Desktop.
We are trying to add a checkbox to each subLayer from a DynamicMapService to switch visibility. We've been able to add a checkbox defined in a LayerTemplate.
So far, we have (based on the runtime .NET sample "LegendsInTreeViewSample):
<esri:Legend x:Name="legend2" Layers="{Binding Converter={StaticResource enumerator}}" ShowOnlyVisibleLayers="False" > <esri:Legend.LayerTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <CheckBox Content="{Binding Label}" IsChecked="{Binding IsEnabled, Mode=TwoWay}"/> </StackPanel> </DataTemplate> </esri:Legend.LayerTemplate> </esri:Legend>
In line 5 the binding of the property "Label" works fine, it shows the layers lable.
Unfortunately it doesn't work with the property "IsEnabled": if one checkbox is cheked/unchecked, all other layers on the same level are also affected .
Has anyone experienced similar behavior?
I have the same question as you. Only, I am trying to show checkboxes on all the lowest levels in the legends. Did you ever get this to work ?
--Brian
Fabian, did you ever get this to work ?
It's been a while, but we cretaed a workaround to make the legend work as desired, at least in our case.
We added a IValueConverter for IsChecked-Property of the CheckBox:
<esri:Legend.LayerTemplate> <DataTemplate> <!--<StackPanel Orientation="Horizontal">--> <CheckBox Content="{Binding Label}" IsChecked="{Binding Converter={StaticResource myLayerVisibilityConverter}, Mode=OneWay}" Tag="{Binding}" Checked="chkLayerTemplate_Checked" Unchecked="chkLayerTemplate_UnChecked"/> <!--</StackPanel>--> </DataTemplate> </esri:Legend.LayerTemplate>
The converter:
public sealed class LayerItemViewModelToVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { LayerItemViewModel _vmodel = value as LayerItemViewModel; if (_vmodel as LayerItemViewModel == null) return true; ArcGISDynamicMapServiceLayer _layerParent = _vmodel.Layer as ArcGISDynamicMapServiceLayer; if (_layerParent == null) return true; bool isVisible = true; int subID = _vmodel.SubLayerID; if (_layerParent.VisibleLayers != null) { if (!_layerParent.VisibleLayers.Contains(subID)) isVisible = false; } return isVisible; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
And in code-behind the checked and unchecked-events of the CheckBox:
private void chkLayerTemplate_Checked(object sender, RoutedEventArgs e) { if (!(sender is CheckBox)) return; Esri.ArcGISRuntime.Toolkit.Controls.Primitives.LayerItemViewModel _layerVM = ((CheckBox)sender).Tag as Esri.ArcGISRuntime.Toolkit.Controls.Primitives.LayerItemViewModel; if (_layerVM == null) return; ArcGISDynamicMapServiceLayer _layerParent = _layerVM.Layer as ArcGISDynamicMapServiceLayer; if (_layerParent == null) return; var subId = _layerVM.SubLayerID; if (_layerParent.VisibleLayers != null) { if (!_layerParent.VisibleLayers.Contains(subId)) _layerParent.VisibleLayers.Add(subId); } } private void chkLayerTemplate_UnChecked(object sender, RoutedEventArgs e) { if (!(sender is CheckBox)) return; Esri.ArcGISRuntime.Toolkit.Controls.Primitives.LayerItemViewModel _layerVM = ((CheckBox)sender).Tag as Esri.ArcGISRuntime.Toolkit.Controls.Primitives.LayerItemViewModel; if (_layerVM == null) return; ArcGISDynamicMapServiceLayer _layerParent = _layerVM.Layer as ArcGISDynamicMapServiceLayer; if (_layerParent == null) { ((CheckBox)sender).IsChecked = true; //no other types return; } var subId = _layerVM.SubLayerID; if (_layerParent.VisibleLayers != null) { if (_layerParent.VisibleLayers.Contains(subId)) _layerParent.VisibleLayers.Remove(subId); } }