public LegendWithTemplates() { InitializeComponent(); LayoutRoot.Children.Add(CreateLegend()); } Legend CreateLegend() { var legend = new Legend { LayerIDs = new[] { "Points of Interest", "United States" }, LayerItemsMode = Legend.Mode.Tree, ShowOnlyVisibleLayers = false, Height = 300, Width = 200, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top, Margin = new Thickness(20) }; legend.SetBinding(Legend.MapProperty, new Binding {ElementName = "MyMap"}); legend.Refreshed += Legend_Refreshed; legend.MapLayerTemplate = CreateTemplate(@" <StackPanel Orientation=""Horizontal""> <CheckBox Content=""{Binding Label}"" IsChecked=""{Binding IsEnabled, Mode=TwoWay}"" IsEnabled=""{Binding IsInScaleRange}"" > </CheckBox> <Slider Maximum=""1"" Value=""{Binding Layer.Opacity, Mode=TwoWay}"" Width=""50"" /> </StackPanel>"); legend.LayerTemplate = CreateTemplate(@" <CheckBox Content=""{Binding Label}"" IsChecked=""{Binding IsEnabled, Mode=TwoWay}"" IsEnabled=""{Binding IsInScaleRange}"" > </CheckBox>"); return legend; } static DataTemplate CreateTemplate(string template) { return (DataTemplate)System.Windows.Markup.XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"">" + template + "</DataTemplate>"); }
Code creating the same legend control than in the sample you pointed out:public LegendWithTemplates() { InitializeComponent(); LayoutRoot.Children.Add(CreateLegend()); } Legend CreateLegend() { var legend = new Legend { LayerIDs = new[] { "Points of Interest", "United States" }, LayerItemsMode = Legend.Mode.Tree, ShowOnlyVisibleLayers = false, Height = 300, Width = 200, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top, Margin = new Thickness(20) }; legend.SetBinding(Legend.MapProperty, new Binding {ElementName = "MyMap"}); legend.Refreshed += Legend_Refreshed; legend.MapLayerTemplate = CreateTemplate(@" <StackPanel Orientation=""Horizontal""> <CheckBox Content=""{Binding Label}"" IsChecked=""{Binding IsEnabled, Mode=TwoWay}"" IsEnabled=""{Binding IsInScaleRange}"" > </CheckBox> <Slider Maximum=""1"" Value=""{Binding Layer.Opacity, Mode=TwoWay}"" Width=""50"" /> </StackPanel>"); legend.LayerTemplate = CreateTemplate(@" <CheckBox Content=""{Binding Label}"" IsChecked=""{Binding IsEnabled, Mode=TwoWay}"" IsEnabled=""{Binding IsInScaleRange}"" > </CheckBox>"); return legend; } static DataTemplate CreateTemplate(string template) { return (DataTemplate)System.Windows.Markup.XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"">" + template + "</DataTemplate>"); }
Your code work correctly to reduce opacity for each layer in my application. But, I have other problem, when I unchecked the checkbox, the layer will disappear.