MapLayer/MapLayerItemsSource

This commit is contained in:
ClemensFischer 2025-08-12 11:33:39 +02:00
parent bb47829cd3
commit 21ac686843
5 changed files with 24 additions and 78 deletions

View file

@ -1,6 +1,4 @@
using System.Collections.Generic; namespace MapControl
namespace MapControl
{ {
public partial class MapPanel public partial class MapPanel
{ {
@ -13,18 +11,6 @@ namespace MapControl
public static readonly AttachedProperty<BoundingBox> BoundingBoxProperty = public static readonly AttachedProperty<BoundingBox> BoundingBoxProperty =
DependencyPropertyHelper.RegisterAttached<BoundingBox>("BoundingBox", typeof(MapPanel)); DependencyPropertyHelper.RegisterAttached<BoundingBox>("BoundingBox", typeof(MapPanel));
protected Controls ChildElements => Children;
protected FrameworkElement GetChildElement(int index) => index < Children.Count ? Children[index] : null;
protected void InsertChildElement(int index, FrameworkElement element) => Children.Insert(index, element);
protected void InsertChildElements(int index, IEnumerable<FrameworkElement> elements) => Children.InsertRange(index, elements);
protected void RemoveChildElement(int index) => Children.RemoveAt(index);
protected void RemoveChildElements(int index, int count) => Children.RemoveRange(index, count);
static MapPanel() static MapPanel()
{ {
AffectsParentArrange<MapPanel>(LocationProperty, BoundingBoxProperty); AffectsParentArrange<MapPanel>(LocationProperty, BoundingBoxProperty);

View file

@ -1,4 +1,5 @@
using System.Collections; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Linq; using System.Linq;
@ -54,14 +55,14 @@ namespace MapControl
private void MapLayerPropertyChanged(object oldLayer, object newLayer) private void MapLayerPropertyChanged(object oldLayer, object newLayer)
{ {
var firstChild = GetChildElement(0); var firstChild = Children.Cast<FrameworkElement>().FirstOrDefault();
if (oldLayer != null) if (oldLayer != null)
{ {
if (firstChild != null && if (firstChild != null &&
(firstChild == oldLayer as FrameworkElement || firstChild.DataContext == oldLayer)) (firstChild == oldLayer as FrameworkElement || firstChild.DataContext == oldLayer))
{ {
RemoveChildElement(0); Children.RemoveAt(0);
} }
if (hasMapLayerBackground) if (hasMapLayerBackground)
@ -83,10 +84,10 @@ namespace MapControl
if (firstChild == null || if (firstChild == null ||
firstChild != newLayer as FrameworkElement && firstChild.DataContext != newLayer) firstChild != newLayer as FrameworkElement && firstChild.DataContext != newLayer)
{ {
InsertChildElement(0, GetMapLayer(newLayer)); Children.Insert(0, GetMapLayer(newLayer));
} }
if (GetChildElement(0) is IMapLayer mapLayer) if (Children.Cast<FrameworkElement>().FirstOrDefault() is IMapLayer mapLayer)
{ {
if (mapLayer.MapBackground != null) if (mapLayer.MapBackground != null)
{ {
@ -168,17 +169,23 @@ namespace MapControl
private void AddMapLayers(List<FrameworkElement> mapLayers, int index) private void AddMapLayers(List<FrameworkElement> mapLayers, int index)
{ {
InsertChildElements(index, mapLayers); foreach (var mapLayer in mapLayers)
{
Children.Insert(index, mapLayer);
if (index == 0) if (index == 0)
{ {
MapLayer = mapLayers[0]; MapLayer = mapLayer;
}
} }
} }
private void RemoveMapLayers(IEnumerable items, int index) private void RemoveMapLayers(IEnumerable items, int index)
{ {
RemoveChildElements(index, items.Cast<object>().Count()); foreach (var _ in items)
{
Children.RemoveAt(index);
}
if (index == 0) if (index == 0)
{ {

View file

@ -1,4 +1,5 @@
#if WPF using System.Linq;
#if WPF
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Media; using System.Windows.Media;
@ -172,7 +173,7 @@ namespace MapControl
{ {
availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity); availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
foreach (var element in ChildElements) foreach (var element in Children.Cast<FrameworkElement>())
{ {
element.Measure(availableSize); element.Measure(availableSize);
} }
@ -184,7 +185,7 @@ namespace MapControl
{ {
if (parentMap != null) if (parentMap != null)
{ {
foreach (var element in ChildElements) foreach (var element in Children.Cast<FrameworkElement>())
{ {
ArrangeChildElement(element, finalSize); ArrangeChildElement(element, finalSize);
} }

View file

@ -1,6 +1,4 @@
using System.Collections.Generic; using System.Windows;
using System.Linq;
using System.Windows;
using System.Windows.Media; using System.Windows.Media;
namespace MapControl namespace MapControl
@ -18,24 +16,6 @@ namespace MapControl
DependencyPropertyHelper.RegisterAttached<BoundingBox>("BoundingBox", typeof(MapPanel), null, DependencyPropertyHelper.RegisterAttached<BoundingBox>("BoundingBox", typeof(MapPanel), null,
FrameworkPropertyMetadataOptions.AffectsParentArrange); FrameworkPropertyMetadataOptions.AffectsParentArrange);
protected IEnumerable<FrameworkElement> ChildElements => Children.OfType<FrameworkElement>();
protected FrameworkElement GetChildElement(int index) => index < Children.Count ? (FrameworkElement)Children[index] : null;
protected void InsertChildElement(int index, FrameworkElement element) => Children.Insert(index, element);
protected void InsertChildElements(int index, IEnumerable<FrameworkElement> elements)
{
foreach (var element in elements)
{
Children.Insert(index++, element);
}
}
protected void RemoveChildElement(int index) => Children.RemoveAt(index);
protected void RemoveChildElements(int index, int count) => Children.RemoveRange(index, count);
public static MapBase GetParentMap(FrameworkElement element) public static MapBase GetParentMap(FrameworkElement element)
{ {
return (MapBase)element.GetValue(ParentMapProperty); return (MapBase)element.GetValue(ParentMapProperty);

View file

@ -1,8 +1,4 @@
using System.Collections.Generic; #if UWP
using System.Linq;
using System.Xml.Linq;
#if UWP
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media;
#else #else
@ -25,30 +21,6 @@ namespace MapControl
DependencyPropertyHelper.RegisterAttached<BoundingBox>("BoundingBox", typeof(MapPanel), null, DependencyPropertyHelper.RegisterAttached<BoundingBox>("BoundingBox", typeof(MapPanel), null,
(element, oldValue, newValue) => (element.Parent as MapPanel)?.InvalidateArrange()); (element, oldValue, newValue) => (element.Parent as MapPanel)?.InvalidateArrange());
protected IEnumerable<FrameworkElement> ChildElements => Children.OfType<FrameworkElement>();
protected FrameworkElement GetChildElement(int index) => index < Children.Count ? (FrameworkElement)Children[index] : null;
protected void InsertChildElement(int index, FrameworkElement element) => Children.Insert(index, element);
protected void InsertChildElements(int index, IEnumerable<FrameworkElement> elements)
{
foreach (var element in elements)
{
Children.Insert(index++, element);
}
}
protected void RemoveChildElement(int index) => Children.RemoveAt(index);
protected void RemoveChildElements(int index, int count)
{
while (--count >= 0)
{
RemoveChildElement(index);
}
}
public static void InitMapElement(FrameworkElement element) public static void InitMapElement(FrameworkElement element)
{ {
// Workaround for missing property value inheritance. // Workaround for missing property value inheritance.