Added MapItemsControl.SelectionGeometry property for WPF.

This commit is contained in:
ClemensF 2012-11-23 23:00:50 +01:00
parent 4fbc5bc00c
commit ac6f9d5647
5 changed files with 76 additions and 22 deletions

View file

@ -12,22 +12,17 @@ namespace MapControl
{ {
partial void Initialize() partial void Initialize()
{ {
#if !SILVERLIGHT
ManipulationDelta += OnManipulationDelta;
#endif
MouseWheel += OnMouseWheel; MouseWheel += OnMouseWheel;
MouseLeftButtonDown += OnMouseLeftButtonDown; MouseLeftButtonDown += OnMouseLeftButtonDown;
MouseLeftButtonUp += OnMouseLeftButtonUp; MouseLeftButtonUp += OnMouseLeftButtonUp;
MouseMove += OnMouseMove; MouseMove += OnMouseMove;
}
#if !SILVERLIGHT #if !SILVERLIGHT
private void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e) ManipulationDelta += (o, e) => TransformMap(
{ e.ManipulationOrigin, (Point)e.DeltaManipulation.Translation, e.DeltaManipulation.Rotation,
var d = e.DeltaManipulation; (e.DeltaManipulation.Scale.X + e.DeltaManipulation.Scale.Y) / 2d); ;
TransformMap(e.ManipulationOrigin, (Point)d.Translation, d.Rotation, (d.Scale.X + d.Scale.Y) / 2d);
}
#endif #endif
}
private void OnMouseWheel(object sender, MouseWheelEventArgs e) private void OnMouseWheel(object sender, MouseWheelEventArgs e)
{ {

View file

@ -2,12 +2,6 @@
// Copyright © 2012 Clemens Fischer // Copyright © 2012 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL) // Licensed under the Microsoft Public License (Ms-PL)
#if WINRT
using Windows.UI.Xaml;
#else
using System.Windows;
#endif
namespace MapControl namespace MapControl
{ {
public partial class MapItemsControl public partial class MapItemsControl

View file

@ -3,13 +3,21 @@
// Licensed under the Microsoft Public License (Ms-PL) // Licensed under the Microsoft Public License (Ms-PL)
using System; using System;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MapControl namespace MapControl
{ {
public partial class MapItemsControl 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( public static readonly DependencyProperty IsCurrentProperty = DependencyProperty.RegisterAttached(
"IsCurrent", typeof(bool), typeof(MapItemsControl), null); "IsCurrent", typeof(bool), typeof(MapItemsControl), null);
@ -25,14 +33,35 @@ namespace MapControl
Items.CurrentChanged += OnCurrentItemChanged; Items.CurrentChanged += OnCurrentItemChanged;
} }
public Geometry SelectionGeometry
{
get { return (Geometry)GetValue(SelectionGeometryProperty); }
set { SetValue(SelectionGeometryProperty, value); }
}
public static bool GetIsCurrent(UIElement element) public static bool GetIsCurrent(UIElement element)
{ {
return (bool)element.GetValue(IsCurrentProperty); 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<object>().FirstOrDefault(i => IsItemInGeometry(i, geometry));
}
public IList<object> GetItemsInGeometry(Geometry geometry)
{
if (geometry == null || geometry.IsEmpty())
{
return new List<object>();
}
return new List<object>(Items.Cast<object>().Where(i => IsItemInGeometry(i, geometry)));
} }
private void OnCurrentItemChanging(object sender, CurrentChangingEventArgs e) private void OnCurrentItemChanging(object sender, CurrentChangingEventArgs e)
@ -41,7 +70,7 @@ namespace MapControl
if (container != null) if (container != null)
{ {
SetIsCurrent(container, false); container.SetValue(IsCurrentProperty, false);
} }
} }
@ -51,8 +80,43 @@ namespace MapControl
if (container != null) 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));
}
} }
} }

View file

@ -14,7 +14,8 @@ namespace MapControl
{ {
/// <summary> /// <summary>
/// Manages a collection of selectable items on a Map. Uses MapItem as container for items /// 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.
/// </summary> /// </summary>
public partial class MapItemsControl : ListBox public partial class MapItemsControl : ListBox
{ {

View file

@ -104,9 +104,9 @@
<EllipseGeometry RadiusX="8" RadiusY="8"/> <EllipseGeometry RadiusX="8" RadiusY="8"/>
</Path.Data> </Path.Data>
</Path> </Path>
<Grid Canvas.Left="10" Canvas.Bottom="3"> <Grid Canvas.Left="15" Canvas.Top="-8">
<Rectangle Name="labelBackground" Fill="White" Opacity="0.7"/> <Rectangle Name="labelBackground" Fill="White" Opacity="0.7"/>
<TextBlock Margin="2,0,2,1" Text="{Binding Name}"/> <TextBlock Margin="2,0,2,0" Text="{Binding Name}"/>
</Grid> </Grid>
</Canvas> </Canvas>
</ControlTemplate> </ControlTemplate>