2012-11-22 21:42:29 +01:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
|
|
|
|
|
// Copyright © 2012 Clemens Fischer
|
|
|
|
|
|
// 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
|
|
|
|
|
|
{
|
|
|
|
|
|
public partial class MapItemsControl
|
|
|
|
|
|
{
|
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
|
|
|
|
public static readonly DependencyProperty IsCurrentProperty = DependencyProperty.RegisterAttached(
|
|
|
|
|
|
"IsCurrent", typeof(bool), typeof(MapItemsControl), null);
|
|
|
|
|
|
|
|
|
|
|
|
static MapItemsControl()
|
|
|
|
|
|
{
|
|
|
|
|
|
FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
|
|
|
|
|
|
typeof(MapItemsControl), new FrameworkPropertyMetadata(typeof(MapItemsControl)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-23 16:16:09 +01:00
|
|
|
|
public MapItemsControl()
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
|
|
|
|
|
Items.CurrentChanging += OnCurrentItemChanging;
|
|
|
|
|
|
Items.CurrentChanged += OnCurrentItemChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-26 19:17:12 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets a Geometry that selects all items that lie inside its fill area,
|
|
|
|
|
|
/// i.e. where Geometry.FillContains returns true for the item's viewport position.
|
|
|
|
|
|
/// </summary>
|
2012-11-23 23:00:50 +01:00
|
|
|
|
public Geometry SelectionGeometry
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (Geometry)GetValue(SelectionGeometryProperty); }
|
|
|
|
|
|
set { SetValue(SelectionGeometryProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
public static bool GetIsCurrent(UIElement element)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (bool)element.GetValue(IsCurrentProperty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-23 23:00:50 +01:00
|
|
|
|
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())
|
|
|
|
|
|
{
|
|
|
|
|
|
return new List<object>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new List<object>(Items.Cast<object>().Where(i => IsItemInGeometry(i, geometry)));
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnCurrentItemChanging(object sender, CurrentChangingEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
var container = ContainerFromItem(Items.CurrentItem);
|
|
|
|
|
|
|
|
|
|
|
|
if (container != null)
|
|
|
|
|
|
{
|
2012-11-23 23:00:50 +01:00
|
|
|
|
container.SetValue(IsCurrentProperty, false);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnCurrentItemChanged(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
var container = ContainerFromItem(Items.CurrentItem);
|
|
|
|
|
|
|
|
|
|
|
|
if (container != null)
|
|
|
|
|
|
{
|
2012-11-23 23:00:50 +01:00
|
|
|
|
container.SetValue(IsCurrentProperty, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void SelectionGeometryPropertyChanged(Geometry geometry)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SelectionMode == SelectionMode.Single)
|
|
|
|
|
|
{
|
|
|
|
|
|
SelectedItem = GetFirstItemInGeometry(geometry);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
SetSelectedItems(GetItemsInGeometry(geometry));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool IsItemInGeometry(object item, Geometry geometry)
|
|
|
|
|
|
{
|
|
|
|
|
|
var container = ContainerFromItem(item);
|
2012-11-26 19:17:12 +01:00
|
|
|
|
Point? viewportPosition;
|
2012-11-23 23:00:50 +01:00
|
|
|
|
|
2012-11-25 21:32:04 +01:00
|
|
|
|
return container != null &&
|
2012-11-26 19:17:12 +01:00
|
|
|
|
(viewportPosition = MapPanel.GetViewportPosition(container)).HasValue &&
|
|
|
|
|
|
geometry.FillContains(viewportPosition.Value);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|