view Chronosv2/source/Extensions/Windows/AdornerExtensions.cs @ 21:dfc81f8bb838

working version for sttocks except ui within metrowpf
author adminsh@apollo
date Tue, 20 Mar 2012 15:07:31 +0000
parents 443821e55f06
children
line wrap: on
line source

using System.Windows;
using System.Windows.Documents;
using System.Windows.Controls;

namespace Chronos.Presentation.Controls.Adorner
{
    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
    }
}