DataGridHeaderBorder.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Themes / Shared / Microsoft / Windows / Themes / DataGridHeaderBorder.cs / 1305600 / DataGridHeaderBorder.cs

                            //---------------------------------------------------------------------------- 
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
//--------------------------------------------------------------------------- 

using System; 
using System.Collections.Generic; 
using System.ComponentModel;
using System.Diagnostics; 
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
 
namespace Microsoft.Windows.Themes
{ 
    ///  
    ///     A Border used to provide the default look of DataGrid headers.
    ///     When Background or BorderBrush are set, the rendering will 
    ///     revert back to the default Border implementation.
    /// 
    public sealed partial class DataGridHeaderBorder : Border
    { 
        static DataGridHeaderBorder()
        { 
            // We always set this to true on these borders, so just default it to true here. 
            SnapsToDevicePixelsProperty.OverrideMetadata(typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(true));
        } 

        #region Header Appearance Properties

        ///  
        ///     Whether the hover look should be applied.
        ///  
        public bool IsHovered 
        {
            get { return (bool)GetValue(IsHoveredProperty); } 
            set { SetValue(IsHoveredProperty, value); }
        }

        ///  
        ///     DependencyProperty for IsHovered.
        ///  
        public static readonly DependencyProperty IsHoveredProperty = 
            DependencyProperty.Register("IsHovered", typeof(bool), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
 
        /// 
        ///     Whether the pressed look should be applied.
        /// 
        public bool IsPressed 
        {
            get { return (bool)GetValue(IsPressedProperty); } 
            set { SetValue(IsPressedProperty, value); } 
        }
 
        /// 
        ///     DependencyProperty for IsPressed.
        /// 
        public static readonly DependencyProperty IsPressedProperty = 
            DependencyProperty.Register("IsPressed", typeof(bool), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsArrange));
 
        ///  
        ///     When false, will not apply the hover look even when IsHovered is true.
        ///  
        public bool IsClickable
        {
            get { return (bool)GetValue(IsClickableProperty); }
            set { SetValue(IsClickableProperty, value); } 
        }
 
        ///  
        ///     DependencyProperty for IsClickable.
        ///  
        public static readonly DependencyProperty IsClickableProperty =
            DependencyProperty.Register("IsClickable", typeof(bool), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsArrange));

        ///  
        ///     Whether to appear sorted.
        ///  
        public ListSortDirection? SortDirection 
        {
            get { return (ListSortDirection?)GetValue(SortDirectionProperty); } 
            set { SetValue(SortDirectionProperty, value); }
        }

        ///  
        ///     DependencyProperty for SortDirection.
        ///  
        public static readonly DependencyProperty SortDirectionProperty = 
            DependencyProperty.Register("SortDirection", typeof(ListSortDirection?), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
 
        /// 
        ///     Whether to appear selected.
        /// 
        public bool IsSelected 
        {
            get { return (bool)GetValue(IsSelectedProperty); } 
            set { SetValue(IsSelectedProperty, value); } 
        }
 
        /// 
        ///     DependencyProperty for IsSelected.
        /// 
        public static readonly DependencyProperty IsSelectedProperty = 
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
 
        ///  
        ///     Vertical = column header
        ///     Horizontal = row header 
        /// 
        public Orientation Orientation
        {
            get { return (Orientation)GetValue(OrientationProperty); } 
            set { SetValue(OrientationProperty, value); }
        } 
 
        /// 
        ///     DependencyProperty for Orientation. 
        /// 
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(Orientation), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(Orientation.Vertical, FrameworkPropertyMetadataOptions.AffectsRender));
 
        /// 
        ///     When there is a Background or BorderBrush, revert to the Border implementation. 
        ///  
        private bool UsingBorderImplementation
        { 
            get
            {
                return (Background != null) || (BorderBrush != null);
            } 
        }
 
        ///  
        ///     Property that indicates the brush to use when drawing seperators between headers.
        ///  
        public Brush SeparatorBrush
        {
            get { return (Brush)GetValue(SeparatorBrushProperty); }
            set { SetValue(SeparatorBrushProperty, value); } 
        }
 
        ///  
        ///     DependencyProperty for SeparatorBrush.
        ///  
        public static readonly DependencyProperty SeparatorBrushProperty =
            DependencyProperty.Register("SeparatorBrush", typeof(Brush), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(null));

        ///  
        ///     Property that indicates the Visibility for the header seperators.
        ///  
        public Visibility SeparatorVisibility 
        {
            get { return (Visibility)GetValue(SeparatorVisibilityProperty); } 
            set { SetValue(SeparatorVisibilityProperty, value); }
        }

        ///  
        ///     DependencyProperty for SeperatorBrush.
        ///  
        public static readonly DependencyProperty SeparatorVisibilityProperty = 
            DependencyProperty.Register("SeparatorVisibility", typeof(Visibility), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(Visibility.Visible));
 
        #endregion

        #region Layout
 
        /// 
        ///     Calculates the desired size of the element given the constraint. 
        ///  
        protected override Size MeasureOverride(Size constraint)
        { 
            if (UsingBorderImplementation)
            {
                // Revert to the Border implementation
                return base.MeasureOverride(constraint); 
            }
 
            UIElement child = Child; 
            if (child != null)
            { 
                // Use the public Padding property if it's set
                Thickness padding = Padding;
                if (padding.Equals(new Thickness()))
                { 
                    padding = DefaultPadding;
                } 
 
                double childWidth = constraint.Width;
                double childHeight = constraint.Height; 

                // If there is an actual constraint, then reserve space for the chrome
                if (!Double.IsInfinity(childWidth))
                { 
                    childWidth = Math.Max(0.0, childWidth - padding.Left - padding.Right);
                } 
 
                if (!Double.IsInfinity(childHeight))
                { 
                    childHeight = Math.Max(0.0, childHeight - padding.Top - padding.Bottom);
                }

                child.Measure(new Size(childWidth, childHeight)); 
                Size desiredSize = child.DesiredSize;
 
                // Add on the reserved space for the chrome 
                return new Size(desiredSize.Width + padding.Left + padding.Right, desiredSize.Height + padding.Top + padding.Bottom);
            } 

            return new Size();
        }
 
        /// 
        ///     Positions children and returns the final size of the element. 
        ///  
        protected override Size ArrangeOverride(Size arrangeSize)
        { 
            if (UsingBorderImplementation)
            {
                // Revert to the Border implementation
                return base.ArrangeOverride(arrangeSize); 
            }
 
            UIElement child = Child; 
            if (child != null)
            { 
                // Use the public Padding property if it's set
                Thickness padding = Padding;
                if (padding.Equals(new Thickness()))
                { 
                    padding = DefaultPadding;
                } 
 
                // Reserve space for the chrome
                double childWidth = Math.Max(0.0, arrangeSize.Width - padding.Left - padding.Right); 
                double childHeight = Math.Max(0.0, arrangeSize.Height - padding.Top - padding.Bottom);

                child.Arrange(new Rect(padding.Left, padding.Top, childWidth, childHeight));
            } 

            return arrangeSize; 
        } 

        #endregion 

        #region Rendering

        ///  
        ///     Returns a default padding for the various themes for use
        ///     by measure and arrange. 
        ///  
        private Thickness DefaultPadding
        { 
            get
            {
                Thickness padding = new Thickness(3.0); // The default padding
                Thickness? themePadding = ThemeDefaultPadding; 
                if (themePadding == null)
                { 
                    if (Orientation == Orientation.Vertical) 
                    {
                        // Reserve space to the right for the arrow 
                        padding.Right = 15.0;
                    }
                }
                else 
                {
                    padding = (Thickness)themePadding; 
                } 

                // When pressed, offset the child 
                if (IsPressed && IsClickable)
                {
                    padding.Left += 1.0;
                    padding.Top += 1.0; 
                    padding.Right -= 1.0;
                    padding.Bottom -= 1.0; 
                } 

                return padding; 
            }
        }

        ///  
        ///     Called when this element should re-render.
        ///  
        protected override void OnRender(DrawingContext dc) 
        {
            if (UsingBorderImplementation) 
            {
                // Revert to the Border implementation
                base.OnRender(dc);
            } 
            else
            { 
                RenderTheme(dc); 
            }
        } 

        private static double Max0(double d)
        {
            return Math.Max(0.0, d); 
        }
 
        #endregion 

        #region Freezable Cache 

        /// 
        ///     Creates a cache of frozen Freezable resources for use
        ///     across all instances of the border. 
        /// 
        private static void EnsureCache(int size) 
        { 
            // Quick check to avoid locking
            if (_freezableCache == null) 
            {
                lock (_cacheAccess)
                {
                    // Re-check in case another thread created the cache 
                    if (_freezableCache == null)
                    { 
                        _freezableCache = new List(size); 
                        for (int i = 0; i < size; i++)
                        { 
                            _freezableCache.Add(null);
                        }
                    }
                } 
            }
 
            Debug.Assert(_freezableCache.Count == size, "The cache size does not match the requested amount."); 
        }
 
        /// 
        ///     Releases all resources in the cache.
        /// 
        private static void ReleaseCache() 
        {
            // Avoid locking if necessary 
            if (_freezableCache != null) 
            {
                lock (_cacheAccess) 
                {
                    // No need to re-check if non-null since it's OK to set it to null multiple times
                    _freezableCache = null;
                } 
            }
        } 
 
        /// 
        ///     Retrieves a cached resource. 
        /// 
        private static Freezable GetCachedFreezable(int index)
        {
            lock (_cacheAccess) 
            {
                Freezable freezable = _freezableCache[index]; 
                Debug.Assert((freezable == null) || freezable.IsFrozen, "Cached Freezables should have been frozen."); 
                return freezable;
            } 
        }

        /// 
        ///     Caches a resources. 
        /// 
        private static void CacheFreezable(Freezable freezable, int index) 
        { 
            Debug.Assert(freezable.IsFrozen, "Cached Freezables should be frozen.");
 
            lock (_cacheAccess)
            {
                if (_freezableCache[index] != null)
                { 
                    _freezableCache[index] = freezable;
                } 
            } 
        }
 
        private static List _freezableCache;
        private static object _cacheAccess = new object();

        #endregion 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------------------- 
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
//--------------------------------------------------------------------------- 

using System; 
using System.Collections.Generic; 
using System.ComponentModel;
using System.Diagnostics; 
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
 
namespace Microsoft.Windows.Themes
{ 
    ///  
    ///     A Border used to provide the default look of DataGrid headers.
    ///     When Background or BorderBrush are set, the rendering will 
    ///     revert back to the default Border implementation.
    /// 
    public sealed partial class DataGridHeaderBorder : Border
    { 
        static DataGridHeaderBorder()
        { 
            // We always set this to true on these borders, so just default it to true here. 
            SnapsToDevicePixelsProperty.OverrideMetadata(typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(true));
        } 

        #region Header Appearance Properties

        ///  
        ///     Whether the hover look should be applied.
        ///  
        public bool IsHovered 
        {
            get { return (bool)GetValue(IsHoveredProperty); } 
            set { SetValue(IsHoveredProperty, value); }
        }

        ///  
        ///     DependencyProperty for IsHovered.
        ///  
        public static readonly DependencyProperty IsHoveredProperty = 
            DependencyProperty.Register("IsHovered", typeof(bool), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
 
        /// 
        ///     Whether the pressed look should be applied.
        /// 
        public bool IsPressed 
        {
            get { return (bool)GetValue(IsPressedProperty); } 
            set { SetValue(IsPressedProperty, value); } 
        }
 
        /// 
        ///     DependencyProperty for IsPressed.
        /// 
        public static readonly DependencyProperty IsPressedProperty = 
            DependencyProperty.Register("IsPressed", typeof(bool), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsArrange));
 
        ///  
        ///     When false, will not apply the hover look even when IsHovered is true.
        ///  
        public bool IsClickable
        {
            get { return (bool)GetValue(IsClickableProperty); }
            set { SetValue(IsClickableProperty, value); } 
        }
 
        ///  
        ///     DependencyProperty for IsClickable.
        ///  
        public static readonly DependencyProperty IsClickableProperty =
            DependencyProperty.Register("IsClickable", typeof(bool), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsArrange));

        ///  
        ///     Whether to appear sorted.
        ///  
        public ListSortDirection? SortDirection 
        {
            get { return (ListSortDirection?)GetValue(SortDirectionProperty); } 
            set { SetValue(SortDirectionProperty, value); }
        }

        ///  
        ///     DependencyProperty for SortDirection.
        ///  
        public static readonly DependencyProperty SortDirectionProperty = 
            DependencyProperty.Register("SortDirection", typeof(ListSortDirection?), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
 
        /// 
        ///     Whether to appear selected.
        /// 
        public bool IsSelected 
        {
            get { return (bool)GetValue(IsSelectedProperty); } 
            set { SetValue(IsSelectedProperty, value); } 
        }
 
        /// 
        ///     DependencyProperty for IsSelected.
        /// 
        public static readonly DependencyProperty IsSelectedProperty = 
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
 
        ///  
        ///     Vertical = column header
        ///     Horizontal = row header 
        /// 
        public Orientation Orientation
        {
            get { return (Orientation)GetValue(OrientationProperty); } 
            set { SetValue(OrientationProperty, value); }
        } 
 
        /// 
        ///     DependencyProperty for Orientation. 
        /// 
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(Orientation), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(Orientation.Vertical, FrameworkPropertyMetadataOptions.AffectsRender));
 
        /// 
        ///     When there is a Background or BorderBrush, revert to the Border implementation. 
        ///  
        private bool UsingBorderImplementation
        { 
            get
            {
                return (Background != null) || (BorderBrush != null);
            } 
        }
 
        ///  
        ///     Property that indicates the brush to use when drawing seperators between headers.
        ///  
        public Brush SeparatorBrush
        {
            get { return (Brush)GetValue(SeparatorBrushProperty); }
            set { SetValue(SeparatorBrushProperty, value); } 
        }
 
        ///  
        ///     DependencyProperty for SeparatorBrush.
        ///  
        public static readonly DependencyProperty SeparatorBrushProperty =
            DependencyProperty.Register("SeparatorBrush", typeof(Brush), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(null));

        ///  
        ///     Property that indicates the Visibility for the header seperators.
        ///  
        public Visibility SeparatorVisibility 
        {
            get { return (Visibility)GetValue(SeparatorVisibilityProperty); } 
            set { SetValue(SeparatorVisibilityProperty, value); }
        }

        ///  
        ///     DependencyProperty for SeperatorBrush.
        ///  
        public static readonly DependencyProperty SeparatorVisibilityProperty = 
            DependencyProperty.Register("SeparatorVisibility", typeof(Visibility), typeof(DataGridHeaderBorder), new FrameworkPropertyMetadata(Visibility.Visible));
 
        #endregion

        #region Layout
 
        /// 
        ///     Calculates the desired size of the element given the constraint. 
        ///  
        protected override Size MeasureOverride(Size constraint)
        { 
            if (UsingBorderImplementation)
            {
                // Revert to the Border implementation
                return base.MeasureOverride(constraint); 
            }
 
            UIElement child = Child; 
            if (child != null)
            { 
                // Use the public Padding property if it's set
                Thickness padding = Padding;
                if (padding.Equals(new Thickness()))
                { 
                    padding = DefaultPadding;
                } 
 
                double childWidth = constraint.Width;
                double childHeight = constraint.Height; 

                // If there is an actual constraint, then reserve space for the chrome
                if (!Double.IsInfinity(childWidth))
                { 
                    childWidth = Math.Max(0.0, childWidth - padding.Left - padding.Right);
                } 
 
                if (!Double.IsInfinity(childHeight))
                { 
                    childHeight = Math.Max(0.0, childHeight - padding.Top - padding.Bottom);
                }

                child.Measure(new Size(childWidth, childHeight)); 
                Size desiredSize = child.DesiredSize;
 
                // Add on the reserved space for the chrome 
                return new Size(desiredSize.Width + padding.Left + padding.Right, desiredSize.Height + padding.Top + padding.Bottom);
            } 

            return new Size();
        }
 
        /// 
        ///     Positions children and returns the final size of the element. 
        ///  
        protected override Size ArrangeOverride(Size arrangeSize)
        { 
            if (UsingBorderImplementation)
            {
                // Revert to the Border implementation
                return base.ArrangeOverride(arrangeSize); 
            }
 
            UIElement child = Child; 
            if (child != null)
            { 
                // Use the public Padding property if it's set
                Thickness padding = Padding;
                if (padding.Equals(new Thickness()))
                { 
                    padding = DefaultPadding;
                } 
 
                // Reserve space for the chrome
                double childWidth = Math.Max(0.0, arrangeSize.Width - padding.Left - padding.Right); 
                double childHeight = Math.Max(0.0, arrangeSize.Height - padding.Top - padding.Bottom);

                child.Arrange(new Rect(padding.Left, padding.Top, childWidth, childHeight));
            } 

            return arrangeSize; 
        } 

        #endregion 

        #region Rendering

        ///  
        ///     Returns a default padding for the various themes for use
        ///     by measure and arrange. 
        ///  
        private Thickness DefaultPadding
        { 
            get
            {
                Thickness padding = new Thickness(3.0); // The default padding
                Thickness? themePadding = ThemeDefaultPadding; 
                if (themePadding == null)
                { 
                    if (Orientation == Orientation.Vertical) 
                    {
                        // Reserve space to the right for the arrow 
                        padding.Right = 15.0;
                    }
                }
                else 
                {
                    padding = (Thickness)themePadding; 
                } 

                // When pressed, offset the child 
                if (IsPressed && IsClickable)
                {
                    padding.Left += 1.0;
                    padding.Top += 1.0; 
                    padding.Right -= 1.0;
                    padding.Bottom -= 1.0; 
                } 

                return padding; 
            }
        }

        ///  
        ///     Called when this element should re-render.
        ///  
        protected override void OnRender(DrawingContext dc) 
        {
            if (UsingBorderImplementation) 
            {
                // Revert to the Border implementation
                base.OnRender(dc);
            } 
            else
            { 
                RenderTheme(dc); 
            }
        } 

        private static double Max0(double d)
        {
            return Math.Max(0.0, d); 
        }
 
        #endregion 

        #region Freezable Cache 

        /// 
        ///     Creates a cache of frozen Freezable resources for use
        ///     across all instances of the border. 
        /// 
        private static void EnsureCache(int size) 
        { 
            // Quick check to avoid locking
            if (_freezableCache == null) 
            {
                lock (_cacheAccess)
                {
                    // Re-check in case another thread created the cache 
                    if (_freezableCache == null)
                    { 
                        _freezableCache = new List(size); 
                        for (int i = 0; i < size; i++)
                        { 
                            _freezableCache.Add(null);
                        }
                    }
                } 
            }
 
            Debug.Assert(_freezableCache.Count == size, "The cache size does not match the requested amount."); 
        }
 
        /// 
        ///     Releases all resources in the cache.
        /// 
        private static void ReleaseCache() 
        {
            // Avoid locking if necessary 
            if (_freezableCache != null) 
            {
                lock (_cacheAccess) 
                {
                    // No need to re-check if non-null since it's OK to set it to null multiple times
                    _freezableCache = null;
                } 
            }
        } 
 
        /// 
        ///     Retrieves a cached resource. 
        /// 
        private static Freezable GetCachedFreezable(int index)
        {
            lock (_cacheAccess) 
            {
                Freezable freezable = _freezableCache[index]; 
                Debug.Assert((freezable == null) || freezable.IsFrozen, "Cached Freezables should have been frozen."); 
                return freezable;
            } 
        }

        /// 
        ///     Caches a resources. 
        /// 
        private static void CacheFreezable(Freezable freezable, int index) 
        { 
            Debug.Assert(freezable.IsFrozen, "Cached Freezables should be frozen.");
 
            lock (_cacheAccess)
            {
                if (_freezableCache[index] != null)
                { 
                    _freezableCache[index] = freezable;
                } 
            } 
        }
 
        private static List _freezableCache;
        private static object _cacheAccess = new object();

        #endregion 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK