view MetroWpf/MetroWpf.Xaml/Extensions/AdornerExtensions.cs @ 100:9b6855dc165e

Updated System.Windows into Libs
author stevenhollidge <stevenhollidge@hotmail.com>
date Sat, 05 May 2012 23:04:16 +0100
parents a8b50a087544
children
line wrap: on
line source

using System;
using System.Collections.Generic;
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
  }
}