// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// Copyright © 2023 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
#if WINUI
using Windows.Foundation;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Media;
#elif UWP
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
#else
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
#endif
namespace MapControl
{
///
/// Container class for an item in a MapItemsControl.
///
public partial class MapItem : ListBoxItem
{
///
/// Gets/sets MapPanel.AutoCollapse.
///
public bool AutoCollapse
{
get => (bool)GetValue(AutoCollapseProperty);
set => SetValue(AutoCollapseProperty, value);
}
///
/// Gets/sets MapPanel.Location.
///
public Location Location
{
get => (Location)GetValue(LocationProperty);
set => SetValue(LocationProperty, value);
}
///
/// Gets a Transform for scaling and rotating geometries
/// in map coordinates (meters) to view coordinates (pixels).
///
public Transform MapTransform => mapTransform ?? (mapTransform = new MatrixTransform());
private MatrixTransform mapTransform;
protected override Size ArrangeOverride(Size bounds)
{
// If MapTransform is used, update its Matrix property.
//
if (mapTransform != null)
{
var parentMap = (VisualTreeHelper.GetParent(this) as MapPanel)?.ParentMap;
if (parentMap != null && Location != null)
{
var scale = parentMap.GetScale(Location);
var matrix = new Matrix(scale.X, 0d, 0d, scale.Y, 0d, 0d);
matrix.Rotate(parentMap.ViewTransform.Rotation);
mapTransform.Matrix = matrix;
}
}
return base.ArrangeOverride(bounds);
}
}
///
/// Manages a collection of selectable items on a Map.
///
public partial class MapItemsControl : ListBox
{
public static readonly DependencyProperty LocationMemberPathProperty = DependencyProperty.Register(
nameof(LocationMemberPath), typeof(string), typeof(MapItemsControl), new PropertyMetadata(null));
///
/// Path to a source property for binding the Location property of MapItem containers.
///
public string LocationMemberPath
{
get => (string)GetValue(LocationMemberPathProperty);
set => SetValue(LocationMemberPathProperty, value);
}
protected override DependencyObject GetContainerForItemOverride()
{
return new MapItem();
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is MapItem;
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
if (LocationMemberPath != null && element is MapItem mapItem)
{
mapItem.SetBinding(MapItem.LocationProperty,
new Binding
{
Path = new PropertyPath(LocationMemberPath),
Source = item
});
}
}
protected override void ClearContainerForItemOverride(DependencyObject element, object item)
{
base.ClearContainerForItemOverride(element, item);
if (LocationMemberPath != null && element is MapItem mapItem)
{
mapItem.ClearValue(MapItem.LocationProperty);
}
}
public void SelectItems(Predicate