Click here to Skip to main content
15,891,431 members
Articles / Desktop Programming / XAML

Build Your Own DataGrid for Silverlight: Step 3

Rate me:
Please Sign up or sign in to vote.
4.07/5 (6 votes)
4 Jun 2009CPOL43 min read 45.5K   2.5K   17  
Add headers to your data grid.
// Copyright 2008-2009 NETiKA TECH.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Windows;

namespace Open.Windows.Controls
{
    /// <summary>
    /// Item to use inside a HandyContainer control. 
    /// </summary>
    public partial class ContainerItem : HandyListItem
    {
        public static readonly DependencyProperty HandyStyleProperty;
        public static readonly DependencyProperty HandyOverflowedStyleProperty;

        static ContainerItem()
        {
            HandyStyleProperty = DependencyProperty.Register("HandyStyle", typeof(HandyContainerItemStyle), typeof(ContainerItem), new PropertyMetadata(new PropertyChangedCallback(OnHandyStyleChanged)));
            HandyOverflowedStyleProperty = DependencyProperty.Register("HandyOverflowedStyle", typeof(HandyContainerItemStyle), typeof(ContainerItem), new PropertyMetadata(new PropertyChangedCallback(OnHandyOverflowedStyleChanged)));
        }

        public ContainerItem()
        {
            this.DefaultStyleKey = typeof(ContainerItem);
        }

        /// <summary>
        /// The Style to apply to the item  
        /// </summary>
        public HandyContainerItemStyle HandyStyle
        {
            get
            {
                return (HandyContainerItemStyle)this.GetValue(HandyStyleProperty);
            }
            set
            {
                base.SetValue(HandyStyleProperty, value);
            }
        }

        private static void OnHandyStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ContainerItem item = (ContainerItem)d;

            item.ApplyHandyStyle();
        }

        private void ApplyHandyStyle()
        {
            if (HandyStyle == HandyContainerItemStyle.None)
            {
                HasInternalStyle = false;
                return;
            }

            HandyContainer container = ManagedItemsControl as HandyContainer;
            if (container != null)
            {
                Style style = container.GetItemStyle(HandyStyle);
                if (style != null)
                {
                    if (this.Style == null)
                    {
                        this.Style = style;
                        OnHandyStyleApplied(EventArgs.Empty);
                    }
                }
            }

            HasInternalStyle = true;            
        }

        /// <summary>
        /// The Style to apply to the item if it is displayed in an OverflowControl.  
        /// </summary>
        public HandyContainerItemStyle HandyOverflowedStyle
        {
            get
            {
                return (HandyContainerItemStyle)this.GetValue(HandyOverflowedStyleProperty);
            }
            set
            {
                base.SetValue(HandyOverflowedStyleProperty, value);
            }
        }

        private static void OnHandyOverflowedStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ContainerItem item = (ContainerItem)d;

            item.ApplyHandyOverflowedStyle();
        }

        private void ApplyHandyOverflowedStyle()
        {
            if (HandyOverflowedStyle == HandyContainerItemStyle.None)
                return;

            HandyContainer container = ManagedItemsControl as HandyContainer;
            if (container != null)
            {
                Style style = container.GetItemStyle(HandyOverflowedStyle);

                if (style != null)
                {
                    if (this.OverflowedItemStyle == null)
                        this.OverflowedItemStyle = style;
                }
            }
        }

        private void _OnApplyTemplate()
        {
            if ((this.Style == null) && (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)))
                throw new NotSupportedException("ContainerItem style is null. Please apply a style to the item either using the DefaultItemStyle or the HandyDefaultItemStyle of its container. A frequent mistake is to use a ContainerItem inside a HandyNavigator or a HandyCommand.");

        }

        protected override void OnManagedItemsControlChanged(EventArgs e)
        {
            base.OnManagedItemsControlChanged(e);

            ApplyHandyStyle();
            ApplyHandyOverflowedStyle();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions