annotate MetroWpf/MetroWpf.Xaml/Extensions/AdornerExtensions.cs @ 15:060f02cd4591

Initial commit, pre airport work
author stevenh7776 stevenhollidge@hotmail.com
date Mon, 12 Mar 2012 23:05:21 +0800
parents
children a8b50a087544
rev   line source
15
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
1 using System;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
2 using System.Collections.Generic;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
3 using System.Linq;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
4 using System.Text;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
5 using System.Windows.Documents;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
6 using System.Windows;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
7 using System.Windows.Controls;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
8
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
9 namespace MetroWpf.Xaml.Extensions
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
10 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
11 public static class AdornerExtensions
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
12 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
13 #region · Extension Methods ·
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
14
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
15 public static void TryRemoveAdorners<T>(this UIElement elem)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
16 where T : System.Windows.Documents.Adorner
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
17 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
18 AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(elem);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
19
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
20 if (adornerLayer != null)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
21 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
22 adornerLayer.RemoveAdorners<T>(elem);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
23 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
24 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
25
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
26 public static void RemoveAdorners<T>(this AdornerLayer adr, UIElement elem)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
27 where T : System.Windows.Documents.Adorner
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
28 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
29 System.Windows.Documents.Adorner[] adorners = adr.GetAdorners(elem);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
30
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
31 if (adorners == null)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
32 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
33 return;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
34 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
35
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
36 for (int i = adorners.Length - 1; i >= 0; i--)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
37 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
38 if (adorners[i] is T)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
39 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
40 adr.Remove(adorners[i]);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
41 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
42 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
43 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
44
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
45 public static void TryAddAdorner<T>(this UIElement elem, System.Windows.Documents.Adorner adorner)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
46 where T : System.Windows.Documents.Adorner
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
47 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
48 AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(elem);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
49
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
50 if (adornerLayer != null && !adornerLayer.ContainsAdorner<T>(elem))
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
51 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
52 adornerLayer.Add(adorner);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
53 Panel.SetZIndex(adornerLayer, Panel.GetZIndex(elem));
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
54 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
55 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
56
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
57 public static bool ContainsAdorner<T>(this AdornerLayer adr, UIElement elem)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
58 where T : System.Windows.Documents.Adorner
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
59 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
60 System.Windows.Documents.Adorner[] adorners = adr.GetAdorners(elem);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
61
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
62 if (adorners == null)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
63 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
64 return false;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
65 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
66
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
67 for (int i = adorners.Length - 1; i >= 0; i--)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
68 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
69 if (adorners[i] is T)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
70 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
71 return true;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
72 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
73 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
74
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
75 return false;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
76 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
77
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
78 public static void RemoveAllAdorners(this AdornerLayer adr, UIElement elem)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
79 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
80 System.Windows.Documents.Adorner[] adorners = adr.GetAdorners(elem);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
81
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
82 if (adorners == null)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
83 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
84 return;
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
85 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
86
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
87 foreach (var toRemove in adorners)
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
88 {
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
89 adr.Remove(toRemove);
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
90 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
91 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
92
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
93 #endregion
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
94 }
060f02cd4591 Initial commit, pre airport work
stevenh7776 stevenhollidge@hotmail.com
parents:
diff changeset
95 }