diff --git a/MapControl/Map.Silverlight.WPF.cs b/MapControl/Map.Silverlight.WPF.cs index 11c337a9..1a0d8b55 100644 --- a/MapControl/Map.Silverlight.WPF.cs +++ b/MapControl/Map.Silverlight.WPF.cs @@ -12,22 +12,17 @@ namespace MapControl { partial void Initialize() { -#if !SILVERLIGHT - ManipulationDelta += OnManipulationDelta; -#endif MouseWheel += OnMouseWheel; MouseLeftButtonDown += OnMouseLeftButtonDown; MouseLeftButtonUp += OnMouseLeftButtonUp; MouseMove += OnMouseMove; - } #if !SILVERLIGHT - private void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e) - { - var d = e.DeltaManipulation; - TransformMap(e.ManipulationOrigin, (Point)d.Translation, d.Rotation, (d.Scale.X + d.Scale.Y) / 2d); - } + ManipulationDelta += (o, e) => TransformMap( + e.ManipulationOrigin, (Point)e.DeltaManipulation.Translation, e.DeltaManipulation.Rotation, + (e.DeltaManipulation.Scale.X + e.DeltaManipulation.Scale.Y) / 2d); ; #endif + } private void OnMouseWheel(object sender, MouseWheelEventArgs e) { diff --git a/MapControl/MapItemsControl.Silverlight.WinRT.cs b/MapControl/MapItemsControl.Silverlight.WinRT.cs index 0d92d1be..de7b51fb 100644 --- a/MapControl/MapItemsControl.Silverlight.WinRT.cs +++ b/MapControl/MapItemsControl.Silverlight.WinRT.cs @@ -2,12 +2,6 @@ // Copyright © 2012 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) -#if WINRT -using Windows.UI.Xaml; -#else -using System.Windows; -#endif - namespace MapControl { public partial class MapItemsControl diff --git a/MapControl/MapItemsControl.WPF.cs b/MapControl/MapItemsControl.WPF.cs index 2518ac66..25fcb936 100644 --- a/MapControl/MapItemsControl.WPF.cs +++ b/MapControl/MapItemsControl.WPF.cs @@ -3,13 +3,21 @@ // Licensed under the Microsoft Public License (Ms-PL) using System; +using System.Collections.Generic; using System.ComponentModel; +using System.Linq; using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; namespace MapControl { public partial class MapItemsControl { + public static readonly DependencyProperty SelectionGeometryProperty = DependencyProperty.Register( + "SelectionGeometry", typeof(Geometry), typeof(MapItemsControl), + new PropertyMetadata((o, e) => ((MapItemsControl)o).SelectionGeometryPropertyChanged((Geometry)e.NewValue))); + public static readonly DependencyProperty IsCurrentProperty = DependencyProperty.RegisterAttached( "IsCurrent", typeof(bool), typeof(MapItemsControl), null); @@ -25,14 +33,35 @@ namespace MapControl Items.CurrentChanged += OnCurrentItemChanged; } + public Geometry SelectionGeometry + { + get { return (Geometry)GetValue(SelectionGeometryProperty); } + set { SetValue(SelectionGeometryProperty, value); } + } + public static bool GetIsCurrent(UIElement element) { return (bool)element.GetValue(IsCurrentProperty); } - private static void SetIsCurrent(UIElement element, bool value) + public object GetFirstItemInGeometry(Geometry geometry) { - element.SetValue(IsCurrentProperty, value); + if (geometry == null || geometry.IsEmpty()) + { + return null; + } + + return Items.Cast().FirstOrDefault(i => IsItemInGeometry(i, geometry)); + } + + public IList GetItemsInGeometry(Geometry geometry) + { + if (geometry == null || geometry.IsEmpty()) + { + return new List(); + } + + return new List(Items.Cast().Where(i => IsItemInGeometry(i, geometry))); } private void OnCurrentItemChanging(object sender, CurrentChangingEventArgs e) @@ -41,7 +70,7 @@ namespace MapControl if (container != null) { - SetIsCurrent(container, false); + container.SetValue(IsCurrentProperty, false); } } @@ -51,8 +80,43 @@ namespace MapControl if (container != null) { - SetIsCurrent(container, true); + 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); + if (container == null) + { + return false; + } + + var location = MapPanel.GetLocation(container); + if (location == null) + { + return false; + } + + var parentMap = MapPanel.GetParentMap(container); + if (parentMap == null) + { + return false; + } + + return geometry.FillContains(parentMap.LocationToViewportPoint(location)); + } } } \ No newline at end of file diff --git a/MapControl/MapItemsControl.cs b/MapControl/MapItemsControl.cs index 0415946c..651bb250 100644 --- a/MapControl/MapItemsControl.cs +++ b/MapControl/MapItemsControl.cs @@ -14,7 +14,8 @@ namespace MapControl { /// /// Manages a collection of selectable items on a Map. Uses MapItem as container for items - /// and updates the IsCurrent attached property when the Items.CurrentItem property changes. + /// and (for WPF only) updates the IsCurrent attached property on each MapItem when the + /// Items.CurrentItem property changes. /// public partial class MapItemsControl : ListBox { diff --git a/SampleApps/WpfApplication/MainWindow.xaml b/SampleApps/WpfApplication/MainWindow.xaml index 75520ae5..94870f0b 100644 --- a/SampleApps/WpfApplication/MainWindow.xaml +++ b/SampleApps/WpfApplication/MainWindow.xaml @@ -104,9 +104,9 @@ - + - +