mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-01-05 08:10:15 +01:00
Selection in MapItemsControl
This commit is contained in:
parent
ec13ed260d
commit
d296600b9e
|
|
@ -2,35 +2,23 @@
|
|||
// © 2018 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.Foundation;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using KeyEventArgs = Windows.UI.Xaml.Input.KeyRoutedEventArgs;
|
||||
#else
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
#endif
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
/// <summary>
|
||||
/// Container class for an item in a MapItemsControl.
|
||||
/// </summary>
|
||||
public class MapItem : ListBoxItem
|
||||
{
|
||||
public MapItem()
|
||||
{
|
||||
DefaultStyleKey = typeof(MapItem);
|
||||
|
||||
MapPanel.InitMapElement(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Manages a collection of selectable items on a Map.
|
||||
/// </summary>
|
||||
public class MapItemsControl : ListBox
|
||||
public partial class MapItemsControl : ListBox
|
||||
{
|
||||
public MapItemsControl()
|
||||
{
|
||||
|
|
@ -49,8 +37,102 @@ namespace MapControl
|
|||
return item is MapItem;
|
||||
}
|
||||
|
||||
protected override void OnKeyDown(KeyEventArgs e)
|
||||
public void SelectItems(Func<object, bool> predicate)
|
||||
{
|
||||
if (SelectionMode == SelectionMode.Single)
|
||||
{
|
||||
throw new InvalidOperationException("SelectionMode must not be Single");
|
||||
}
|
||||
|
||||
foreach (var item in Items)
|
||||
{
|
||||
var selected = predicate(item);
|
||||
|
||||
if (selected != SelectedItems.Contains(item))
|
||||
{
|
||||
if (selected)
|
||||
{
|
||||
SelectedItems.Add(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
SelectedItems.Remove(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SelectItemsByLocation(Func<Location, bool> predicate)
|
||||
{
|
||||
SelectItems(item =>
|
||||
{
|
||||
var loc = MapPanel.GetLocation(MapItemFromItem(item));
|
||||
return loc != null && predicate(loc);
|
||||
});
|
||||
}
|
||||
|
||||
public void SelectItemsByPosition(Func<Point, bool> predicate)
|
||||
{
|
||||
SelectItems(item =>
|
||||
{
|
||||
var pos = MapPanel.GetViewportPosition(MapItemFromItem(item));
|
||||
return pos.HasValue && predicate(pos.Value);
|
||||
});
|
||||
}
|
||||
|
||||
public void SelectItemsInRect(Rect rect)
|
||||
{
|
||||
SelectItemsByPosition(p => rect.Contains(p));
|
||||
}
|
||||
|
||||
internal void MapItemClicked(MapItem mapItem, bool controlKey, bool shiftKey)
|
||||
{
|
||||
var item = ItemFromMapItem(mapItem);
|
||||
|
||||
if (SelectionMode == SelectionMode.Single)
|
||||
{
|
||||
// Single -> set only SelectedItem
|
||||
|
||||
if (SelectedItem != item)
|
||||
{
|
||||
SelectedItem = item;
|
||||
}
|
||||
else if (controlKey)
|
||||
{
|
||||
SelectedItem = null;
|
||||
}
|
||||
}
|
||||
else if (SelectionMode == SelectionMode.Multiple || controlKey)
|
||||
{
|
||||
// Multiple or Extended with Ctrl -> toggle item in SelectedItems
|
||||
|
||||
if (SelectedItems.Contains(item))
|
||||
{
|
||||
SelectedItems.Remove(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
SelectedItems.Add(item);
|
||||
}
|
||||
}
|
||||
else if (shiftKey && SelectedItem != null)
|
||||
{
|
||||
// Extended with Shift -> select items in viewport rectangle
|
||||
|
||||
var p1 = MapPanel.GetViewportPosition(MapItemFromItem(SelectedItem));
|
||||
var p2 = MapPanel.GetViewportPosition(mapItem);
|
||||
|
||||
if (p1.HasValue && p2.HasValue)
|
||||
{
|
||||
SelectItemsInRect(new Rect(p1.Value, p2.Value));
|
||||
}
|
||||
}
|
||||
else if (SelectedItem != item)
|
||||
{
|
||||
// Extended without Control or Shift -> set selected item
|
||||
|
||||
SelectedItem = item;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -157,6 +157,7 @@
|
|||
<Compile Include="Map.UWP.cs" />
|
||||
<Compile Include="MapBase.UWP.cs" />
|
||||
<Compile Include="MapGraticule.UWP.cs" />
|
||||
<Compile Include="MapItemsControl.UWP.cs" />
|
||||
<Compile Include="MapOverlay.UWP.cs" />
|
||||
<Compile Include="MapPanel.UWP.cs" />
|
||||
<Compile Include="MapShape.UWP.cs" />
|
||||
|
|
|
|||
44
MapControl/UWP/MapItemsControl.UWP.cs
Normal file
44
MapControl/UWP/MapItemsControl.UWP.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2018 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using Windows.System;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.UI.Xaml.Input;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
/// <summary>
|
||||
/// Container class for an item in a MapItemsControl.
|
||||
/// </summary>
|
||||
public class MapItem : ListBoxItem
|
||||
{
|
||||
public MapItem()
|
||||
{
|
||||
DefaultStyleKey = typeof(MapItem);
|
||||
|
||||
MapPanel.InitMapElement(this);
|
||||
}
|
||||
|
||||
protected override void OnPointerPressed(PointerRoutedEventArgs e)
|
||||
{
|
||||
e.Handled = true;
|
||||
|
||||
(ItemsControl.ItemsControlFromItemContainer(this) as MapItemsControl)?.MapItemClicked(
|
||||
this, e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control), e.KeyModifiers.HasFlag(VirtualKeyModifiers.Shift));
|
||||
}
|
||||
}
|
||||
|
||||
public partial class MapItemsControl
|
||||
{
|
||||
public MapItem MapItemFromItem(object item)
|
||||
{
|
||||
return (MapItem)ContainerFromItem(item);
|
||||
}
|
||||
|
||||
public object ItemFromMapItem(MapItem mapItem)
|
||||
{
|
||||
return ItemFromContainer(mapItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -180,6 +180,7 @@
|
|||
<Compile Include="Map.WPF.cs" />
|
||||
<Compile Include="MapBase.WPF.cs" />
|
||||
<Compile Include="MapGraticule.WPF.cs" />
|
||||
<Compile Include="MapItemsControl.WPF.cs" />
|
||||
<Compile Include="MapMultiPolygon.WPF.cs" />
|
||||
<Compile Include="MapOverlay.WPF.cs" />
|
||||
<Compile Include="MapPanel.WPF.cs" />
|
||||
|
|
|
|||
49
MapControl/WPF/MapItemsControl.WPF.cs
Normal file
49
MapControl/WPF/MapItemsControl.WPF.cs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2018 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
/// <summary>
|
||||
/// Container class for an item in a MapItemsControl.
|
||||
/// </summary>
|
||||
public class MapItem : ListBoxItem
|
||||
{
|
||||
public MapItem()
|
||||
{
|
||||
DefaultStyleKey = typeof(MapItem);
|
||||
|
||||
MapPanel.InitMapElement(this);
|
||||
}
|
||||
|
||||
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
|
||||
{
|
||||
e.Handled = true;
|
||||
|
||||
(ItemsControl.ItemsControlFromItemContainer(this) as MapItemsControl)?.MapItemClicked(
|
||||
this, Keyboard.Modifiers.HasFlag(ModifierKeys.Control), Keyboard.Modifiers.HasFlag(ModifierKeys.Shift));
|
||||
}
|
||||
}
|
||||
|
||||
public partial class MapItemsControl
|
||||
{
|
||||
public MapItem MapItemFromItem(object item)
|
||||
{
|
||||
return (MapItem)ItemContainerGenerator.ContainerFromItem(item);
|
||||
}
|
||||
|
||||
public object ItemFromMapItem(MapItem mapItem)
|
||||
{
|
||||
return ItemContainerGenerator.ItemFromContainer(mapItem);
|
||||
}
|
||||
|
||||
public void SelectItemsInGeometry(Geometry geometry)
|
||||
{
|
||||
SelectItemsByPosition(p => geometry.FillContains(p));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -18,7 +18,7 @@
|
|||
</Setter>
|
||||
</Style>
|
||||
<Style TargetType="map:MapItem">
|
||||
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
|
||||
<Setter Property="Focusable" Value="False"/>
|
||||
<Setter Property="HorizontalAlignment" Value="Stretch"/>
|
||||
<Setter Property="VerticalAlignment" Value="Stretch"/>
|
||||
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
|
||||
|
|
|
|||
Loading…
Reference in a new issue