#if WPF
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
#elif UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
#elif AVALONIA
using Avalonia.Controls;
using Avalonia.Media;
#endif
namespace MapControl
{
///
/// Container class for an item in a MapItemsControl.
///
public partial class MapItem : ListBoxItem, IMapElement
{
public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.Register(nameof(Location), default,
(item, oldValue, newValue) =>
{
MapPanel.SetLocation(item, newValue);
item.UpdateMapTransform();
});
public static readonly DependencyProperty AutoCollapseProperty =
DependencyPropertyHelper.Register(nameof(AutoCollapse), false,
(item, oldValue, newValue) => MapPanel.SetAutoCollapse(item, newValue));
///
/// Gets/sets MapPanel.Location.
///
public Location Location
{
get => (Location)GetValue(LocationProperty);
set => SetValue(LocationProperty, value);
}
///
/// Gets/sets MapPanel.AutoCollapse.
///
public bool AutoCollapse
{
get => (bool)GetValue(AutoCollapseProperty);
set => SetValue(AutoCollapseProperty, value);
}
///
/// Implements IMapElement.ParentMap.
///
public MapBase ParentMap
{
get;
set
{
if (field != null)
{
field.ViewportChanged -= OnViewportChanged;
}
field = value;
if (field != null && MapTransform != null)
{
// Attach ViewportChanged handler only if MapTransform is actually used.
//
field.ViewportChanged += OnViewportChanged;
UpdateMapTransform();
}
}
}
///
/// Gets a Transform for scaling and rotating geometries
/// in map coordinates (meters) to view coordinates (pixels).
///
public MatrixTransform MapTransform
{
get
{
if (field == null)
{
field = new MatrixTransform();
if (ParentMap != null)
{
ParentMap.ViewportChanged += OnViewportChanged;
UpdateMapTransform();
}
}
return field;
}
}
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
{
UpdateMapTransform();
}
private void UpdateMapTransform()
{
if (MapTransform != null && ParentMap != null)
{
MapTransform.Matrix = ParentMap.GetMapToViewTransform(Location);
}
}
}
}