This repository was archived by the owner on Mar 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMapItemsControl.WPF.cs
More file actions
120 lines (102 loc) · 3.93 KB
/
MapItemsControl.WPF.cs
File metadata and controls
120 lines (102 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
// Copyright © 2014 Clemens Fischer
// 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
{
/// <summary>
/// Manages a collection of selectable items on a Map. Uses MapItem as item container class.
/// </summary>
public class MapItemsControl : ListBox
{
public static readonly DependencyProperty SelectionGeometryProperty = DependencyProperty.Register(
"SelectionGeometry", typeof(Geometry), typeof(MapItemsControl),
new PropertyMetadata((o, e) => ((MapItemsControl)o).SelectionGeometryPropertyChanged((Geometry)e.NewValue)));
static MapItemsControl()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(MapItemsControl), new FrameworkPropertyMetadata(typeof(MapItemsControl)));
}
public MapItemsControl()
{
Items.CurrentChanging += CurrentItemChanging;
Items.CurrentChanged += CurrentItemChanged;
}
protected override DependencyObject GetContainerForItemOverride()
{
return new MapItem();
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is MapItem;
}
/// <summary>
/// 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.
/// </summary>
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)
{
if (geometry == null || geometry.IsEmpty())
{
return null;
}
return Items.Cast<object>().Where(i => IsItemInGeometry(i, geometry)).ToList();
}
private bool IsItemInGeometry(object item, Geometry geometry)
{
var container = ItemContainerGenerator.ContainerFromItem(item) as UIElement;
Point? viewportPosition;
return container != null &&
(viewportPosition = MapPanel.GetViewportPosition(container)).HasValue &&
geometry.FillContains(viewportPosition.Value);
}
private void SelectionGeometryPropertyChanged(Geometry geometry)
{
if (SelectionMode == SelectionMode.Single)
{
SelectedItem = GetFirstItemInGeometry(geometry);
}
else
{
SetSelectedItems(GetItemsInGeometry(geometry));
}
}
private void CurrentItemChanging(object sender, CurrentChangingEventArgs e)
{
var container = ItemContainerGenerator.ContainerFromItem(Items.CurrentItem) as UIElement;
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);
}
}
}
}