/// <summary> Updates the list of layers to be shown in the gadget. </summary>
        private void UpdateLayerList()
        {
            LayersStack.Children.Clear();
            LayersStack.RowDefinitions.Clear();
            LayersStack.Margin = new Thickness(1);
            layerIndices.Clear();
            // Since the header size is calculated on the longest layer caption we have to ensure
            // that in case of a layer stack modification the header size is recalculated. We force this
            // by setting 'headerSizeSet' to false.
            headerSizeSet = false;

            foreach (var item in layers.Select((layer, index) => new { layer, index }))
            {
                item.layer.PropertyChanged += layer_PropertyChanged;

                if (LayerListReverted)
                {
                    layerIndices.Add(item.index);
                }
                else
                {
                    layerIndices.Insert(0, item.index);
                }
            }

            foreach (var item in layerIndices.Select((t, index) => new { layer = layers[t], index }))
            {
                LayersStack.RowDefinitions.Add(new RowDefinition());

                var label = new Label {
                    Tag = item.layer.Name, Padding = new Thickness(-1), Margin = LayersStack.Margin
                };

                // opacityBinding is needed for the image and the layer name -> create once and bind it multiple times
                var opacityBinding = new Binding("Opacity")
                {
                    Source = label
                };

                // Create the text block first because the image size depends on the fond size.
                var textBlock = new TextBlock
                {
                    Tag                 = item.layer.Name,
                    Text                = item.layer.Caption,
                    Padding             = new Thickness(2),
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    VerticalAlignment   = VerticalAlignment.Center,
                    FontWeight          = FontWeights.Normal,
                    FontSize            = HeaderText.FontSize
                };

                textBlock.SetBinding(OpacityProperty, opacityBinding);

                if (item.layer.HasSettingsDialog)
                {
                    textBlock.MouseLeftButtonUp += textBlock_MouseLeftButtonUp;
                    textBlock.TextDecorations.Add(TextDecorations.Underline);
                    textBlock.ToolTip = MapLocalizer.GetString(MapStringId.Options);
                }

                var image = new Image
                {
                    Source            = item.layer.Icon ?? DefaultImageSource,
                    Width             = textBlock.FontSize + 8,
                    Height            = textBlock.FontSize + 8,
                    Margin            = new Thickness(2),
                    VerticalAlignment = VerticalAlignment.Center
                };

                image.SetBinding(OpacityProperty, opacityBinding);
                label.Content = image;

                // Einfügen ins Grid
                Grid.SetColumn(label, 0);
                Grid.SetRow(label, item.index);
                LayersStack.Children.Add(label);

                Grid.SetColumn(textBlock, 1);
                Grid.SetRow(textBlock, item.index);
                LayersStack.Children.Add(textBlock);

                var checkBox = new CheckBox
                {
                    IsChecked         = layers.IsVisible(item.layer),
                    Tag               = item.layer.Name,
                    Margin            = new Thickness(3),
                    VerticalAlignment = VerticalAlignment.Center,
                    ToolTip           = MapLocalizer.GetString(MapStringId.Visibility)
                };
                checkBox.Checked   += visibility_Checked;
                checkBox.Unchecked += visibility_Unchecked;
                Grid.SetColumn(checkBox, 2);
                Grid.SetRow(checkBox, item.index);
                LayersStack.Children.Add(checkBox);

                var slider = new Slider {
                    Tag = item.layer.Name, Margin = new Thickness(3), Width = 40, Minimum = 0, Maximum = 100, VerticalAlignment = VerticalAlignment.Center
                };
                // store any slider (used for calculation of header items later on.
                referenceSlider      = slider;
                slider.ValueChanged += slider_ValueChanged;
                slider.Value         = item.layer.Opacity * 100;
                Grid.SetColumn(slider, 4);
                Grid.SetRow(slider, item.index);
                LayersStack.Children.Add(slider);

                if (!(item.layer is ILayerGeoSearch))
                {
                    continue;                                   // IsChecked = null for the existence of an exclusive selectable layer
                }
                // and this layer itself is not exclusive selectable.

                checkBox = new CheckBox
                {
                    IsChecked         = true,
                    Tag               = item.layer.Name,
                    Margin            = new Thickness(3),
                    VerticalAlignment = VerticalAlignment.Center
                };
                // checkBox.IsThreeState = true; // By clicking it can be iterated through all three states.

                checkBox.Checked            += selection_Checked;
                checkBox.Unchecked          += selection_Unchecked;
                checkBox.MouseRightButtonUp += selection_Exclusive;
                checkBox.ToolTip             = MapLocalizer.GetString(MapStringId.Selectability);

                Grid.SetColumn(checkBox, selectionColumn);
                Grid.SetRow(checkBox, item.index);
                LayersStack.Children.Add(checkBox);
            }

            UpdateSelection();
        }