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