#if WPF
using System.Windows.Controls;
using System.Windows.Media;
#elif UWP
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
#elif WINUI
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
{
///
/// 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);
}
///
/// 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 && Location != null)
{
MapTransform.Matrix = ParentMap.GetMapTransform(Location);
}
}
}
}