mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
137 lines
4.5 KiB
C#
137 lines
4.5 KiB
C#
// WPF MapControl - http://wpfmapcontrol.codeplex.com/
|
|
// Copyright © 2012 Clemens Fischer
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
using System;
|
|
using System.ComponentModel;
|
|
using System.Windows;
|
|
using System.Windows.Controls.Primitives;
|
|
using System.Windows.Media;
|
|
|
|
namespace MapControl
|
|
{
|
|
public enum MapItemSelectionMode { Single, Extended }
|
|
|
|
/// <summary>
|
|
/// Manages a collection of selectable items on a Map. Uses MapItem as container for items
|
|
/// and updates the MapItem.IsCurrent property when Items.CurrentItem changes.
|
|
/// </summary>
|
|
public class MapItemsControl : MultiSelector
|
|
{
|
|
public static readonly DependencyProperty SelectionModeProperty = DependencyProperty.Register(
|
|
"SelectionMode", typeof(MapItemSelectionMode), typeof(MapItemsControl),
|
|
new FrameworkPropertyMetadata((o, e) => ((MapItemsControl)o).CanSelectMultipleItems = (MapItemSelectionMode)e.NewValue != MapItemSelectionMode.Single));
|
|
|
|
public static readonly DependencyProperty SelectionGeometryProperty = DependencyProperty.Register(
|
|
"SelectionGeometry", typeof(Geometry), typeof(MapItemsControl),
|
|
new FrameworkPropertyMetadata((o, e) => ((MapItemsControl)o).SelectionGeometryChanged((Geometry)e.NewValue)));
|
|
|
|
public MapItemsControl()
|
|
{
|
|
CanSelectMultipleItems = false;
|
|
Style = (Style)FindResource(typeof(MapItemsControl));
|
|
Items.CurrentChanging += OnCurrentItemChanging;
|
|
Items.CurrentChanged += OnCurrentItemChanged;
|
|
}
|
|
|
|
public MapItemSelectionMode SelectionMode
|
|
{
|
|
get { return (MapItemSelectionMode)GetValue(SelectionModeProperty); }
|
|
set { SetValue(SelectionModeProperty, value); }
|
|
}
|
|
|
|
public Geometry SelectionGeometry
|
|
{
|
|
get { return (Geometry)GetValue(SelectionGeometryProperty); }
|
|
set { SetValue(SelectionGeometryProperty, value); }
|
|
}
|
|
|
|
public MapItem GetMapItem(object item)
|
|
{
|
|
return item != null ? ItemContainerGenerator.ContainerFromItem(item) as MapItem : null;
|
|
}
|
|
|
|
public object GetHitItem(Point point)
|
|
{
|
|
DependencyObject obj = InputHitTest(point) as DependencyObject;
|
|
|
|
while (obj != null)
|
|
{
|
|
if (obj is MapItem)
|
|
{
|
|
return ((MapItem)obj).Item;
|
|
}
|
|
|
|
obj = VisualTreeHelper.GetParent(obj);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
protected override DependencyObject GetContainerForItemOverride()
|
|
{
|
|
return new MapItem();
|
|
}
|
|
|
|
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
|
|
{
|
|
MapItem mapItem = (MapItem)element;
|
|
mapItem.Item = item;
|
|
base.PrepareContainerForItemOverride(element, item);
|
|
}
|
|
|
|
protected override void ClearContainerForItemOverride(DependencyObject element, object item)
|
|
{
|
|
MapItem mapItem = (MapItem)element;
|
|
mapItem.Item = null;
|
|
base.ClearContainerForItemOverride(element, item);
|
|
}
|
|
|
|
private void OnCurrentItemChanging(object sender, CurrentChangingEventArgs eventArgs)
|
|
{
|
|
MapItem mapItem = GetMapItem(Items.CurrentItem);
|
|
|
|
if (mapItem != null)
|
|
{
|
|
mapItem.IsCurrent = false;
|
|
}
|
|
}
|
|
|
|
private void OnCurrentItemChanged(object sender, EventArgs eventArgs)
|
|
{
|
|
MapItem mapItem = GetMapItem(Items.CurrentItem);
|
|
|
|
if (mapItem != null)
|
|
{
|
|
mapItem.IsCurrent = true;
|
|
}
|
|
}
|
|
|
|
private void SelectionGeometryChanged(Geometry geometry)
|
|
{
|
|
if (geometry != null)
|
|
{
|
|
SelectionMode = MapItemSelectionMode.Extended;
|
|
|
|
BeginUpdateSelectedItems();
|
|
SelectedItems.Clear();
|
|
|
|
if (!geometry.IsEmpty())
|
|
{
|
|
foreach (object item in Items)
|
|
{
|
|
MapItem mapItem = GetMapItem(item);
|
|
|
|
if (mapItem != null && MapPanel.HasViewportPosition(mapItem) && geometry.FillContains(MapPanel.GetViewportPosition(mapItem)))
|
|
{
|
|
SelectedItems.Add(item);
|
|
}
|
|
}
|
|
}
|
|
|
|
EndUpdateSelectedItems();
|
|
}
|
|
}
|
|
}
|
|
}
|