diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MetroWpf/MetroWpf.Xaml/Extensions/AdornerExtensions.cs	Mon Mar 12 23:05:21 2012 +0800
@@ -0,0 +1,95 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Windows.Documents;
+using System.Windows;
+using System.Windows.Controls;
+
+namespace MetroWpf.Xaml.Extensions
+{
+  public static class AdornerExtensions
+  {
+    #region · Extension Methods ·
+
+    public static void TryRemoveAdorners<T>(this UIElement elem)
+        where T : System.Windows.Documents.Adorner
+    {
+      AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(elem);
+
+      if (adornerLayer != null)
+      {
+        adornerLayer.RemoveAdorners<T>(elem);
+      }
+    }
+
+    public static void RemoveAdorners<T>(this AdornerLayer adr, UIElement elem)
+        where T : System.Windows.Documents.Adorner
+    {
+      System.Windows.Documents.Adorner[] adorners = adr.GetAdorners(elem);
+
+      if (adorners == null)
+      {
+        return;
+      }
+
+      for (int i = adorners.Length - 1; i >= 0; i--)
+      {
+        if (adorners[i] is T)
+        {
+          adr.Remove(adorners[i]);
+        }
+      }
+    }
+
+    public static void TryAddAdorner<T>(this UIElement elem, System.Windows.Documents.Adorner adorner)
+        where T : System.Windows.Documents.Adorner
+    {
+      AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(elem);
+
+      if (adornerLayer != null && !adornerLayer.ContainsAdorner<T>(elem))
+      {
+        adornerLayer.Add(adorner);
+        Panel.SetZIndex(adornerLayer, Panel.GetZIndex(elem));
+      }
+    }
+
+    public static bool ContainsAdorner<T>(this AdornerLayer adr, UIElement elem)
+        where T : System.Windows.Documents.Adorner
+    {
+      System.Windows.Documents.Adorner[] adorners = adr.GetAdorners(elem);
+
+      if (adorners == null)
+      {
+        return false;
+      }
+
+      for (int i = adorners.Length - 1; i >= 0; i--)
+      {
+        if (adorners[i] is T)
+        {
+          return true;
+        }
+      }
+
+      return false;
+    }
+
+    public static void RemoveAllAdorners(this AdornerLayer adr, UIElement elem)
+    {
+      System.Windows.Documents.Adorner[] adorners = adr.GetAdorners(elem);
+
+      if (adorners == null)
+      {
+        return;
+      }
+
+      foreach (var toRemove in adorners)
+      {
+        adr.Remove(toRemove);
+      }
+    }
+
+    #endregion
+  }
+}