2012-11-22 21:42:29 +01:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
2013-05-07 18:12:25 +02:00
|
|
|
|
// Copyright © Clemens Fischer 2012-2013
|
2012-11-22 21:42:29 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2012-11-23 23:00:50 +01:00
|
|
|
|
using System.Collections.Generic;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using System.ComponentModel;
|
2012-11-23 23:00:50 +01:00
|
|
|
|
using System.Linq;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using System.Windows;
|
2012-11-23 23:00:50 +01:00
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
using System.Windows.Media;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2013-08-17 08:41:11 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Manages a collection of selectable items on a Map. Uses MapItem as item container class.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class MapItemsControl : ListBox
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2012-11-23 23:00:50 +01:00
|
|
|
|
public static readonly DependencyProperty SelectionGeometryProperty = DependencyProperty.Register(
|
|
|
|
|
|
"SelectionGeometry", typeof(Geometry), typeof(MapItemsControl),
|
|
|
|
|
|
new PropertyMetadata((o, e) => ((MapItemsControl)o).SelectionGeometryPropertyChanged((Geometry)e.NewValue)));
|
|
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
static MapItemsControl()
|
|
|
|
|
|
{
|
2013-08-17 08:41:11 +02:00
|
|
|
|
DefaultStyleKeyProperty.OverrideMetadata(
|
2012-11-22 21:42:29 +01:00
|
|
|
|
typeof(MapItemsControl), new FrameworkPropertyMetadata(typeof(MapItemsControl)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-23 16:16:09 +01:00
|
|
|
|
public MapItemsControl()
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2013-08-17 08:41:11 +02:00
|
|
|
|
Items.CurrentChanging += CurrentItemChanging;
|
|
|
|
|
|
Items.CurrentChanged += CurrentItemChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override DependencyObject GetContainerForItemOverride()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new MapItem();
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-26 19:17:12 +01:00
|
|
|
|
/// <summary>
|
2013-08-17 08:41:11 +02:00
|
|
|
|
/// Gets or sets a Geometry that selects all items inside its fill area, i.e.
|
|
|
|
|
|
/// where Geometry.FillContains returns true for the item's viewport position.
|
2012-11-26 19:17:12 +01:00
|
|
|
|
/// </summary>
|
2012-11-23 23:00:50 +01:00
|
|
|
|
public Geometry SelectionGeometry
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (Geometry)GetValue(SelectionGeometryProperty); }
|
|
|
|
|
|
set { SetValue(SelectionGeometryProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public object GetFirstItemInGeometry(Geometry geometry)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (geometry == null || geometry.IsEmpty())
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Items.Cast<object>().FirstOrDefault(i => IsItemInGeometry(i, geometry));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IList<object> GetItemsInGeometry(Geometry geometry)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2012-11-23 23:00:50 +01:00
|
|
|
|
if (geometry == null || geometry.IsEmpty())
|
|
|
|
|
|
{
|
2013-08-17 08:41:11 +02:00
|
|
|
|
return null;
|
2012-11-23 23:00:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-17 08:41:11 +02:00
|
|
|
|
return Items.Cast<object>().Where(i => IsItemInGeometry(i, geometry)).ToList();
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-17 08:41:11 +02:00
|
|
|
|
private bool IsItemInGeometry(object item, Geometry geometry)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2013-08-17 08:41:11 +02:00
|
|
|
|
var container = ItemContainerGenerator.ContainerFromItem(item) as UIElement;
|
|
|
|
|
|
Point? viewportPosition;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2013-08-17 08:41:11 +02:00
|
|
|
|
return container != null &&
|
|
|
|
|
|
(viewportPosition = MapPanel.GetViewportPosition(container)).HasValue &&
|
|
|
|
|
|
geometry.FillContains(viewportPosition.Value);
|
2012-11-23 23:00:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void SelectionGeometryPropertyChanged(Geometry geometry)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SelectionMode == SelectionMode.Single)
|
|
|
|
|
|
{
|
|
|
|
|
|
SelectedItem = GetFirstItemInGeometry(geometry);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
SetSelectedItems(GetItemsInGeometry(geometry));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-17 08:41:11 +02:00
|
|
|
|
private void CurrentItemChanging(object sender, CurrentChangingEventArgs e)
|
2012-11-23 23:00:50 +01:00
|
|
|
|
{
|
2013-08-17 08:41:11 +02:00
|
|
|
|
var container = ItemContainerGenerator.ContainerFromItem(Items.CurrentItem) as UIElement;
|
2012-11-23 23:00:50 +01:00
|
|
|
|
|
2013-08-17 08:41:11 +02:00
|
|
|
|
if (container != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var zIndex = Panel.GetZIndex(container);
|
|
|
|
|
|
Panel.SetZIndex(container, zIndex & ~0x40000000);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CurrentItemChanged(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
var container = ItemContainerGenerator.ContainerFromItem(Items.CurrentItem) as UIElement;
|
|
|
|
|
|
|
|
|
|
|
|
if (container != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var zIndex = Panel.GetZIndex(container);
|
|
|
|
|
|
Panel.SetZIndex(container, zIndex | 0x40000000);
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-08-17 08:41:11 +02:00
|
|
|
|
}
|