Click here to Skip to main content
15,892,059 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;
using System.Windows.Controls;

namespace Open.Windows.Controls
{
    /// <summary>
    /// The HandyListBox control inherits from the Silverlight ListBox. It adds HandyStyle features to it.
    /// </summary>
    public class HandyListBox: ListBox
    {
        public static readonly DependencyProperty HandyStyleProperty;

        static HandyListBox()
        {
            HandyStyleProperty = DependencyProperty.Register("HandyStyle", typeof(HandyListBoxStyle), typeof(HandyListBox), null);
            
        }

        public HandyListBox()
        {
            DefaultStyleKey = typeof(HandyListBox);
            this.LayoutUpdated += new EventHandler(HandyListBox_LayoutUpdated);
            
        }

        private void HandyListBox_LayoutUpdated(object sender, EventArgs e)
        {
            if (!handyStyleApplied)
            {
                this.ApplyHandyStyle();
            }
        }

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


        private bool handyStyleApplied;
        private void ApplyHandyStyle()
        {
            Style style = ResourceHelper.FindResource(Enum.GetName(typeof(HandyListBoxStyle), HandyStyle)) as Style;
            if (style != null)
            {
                if (this.Style == null)
                {
                    this.Style = style;
                    handyStyleApplied = true;
                }
            }
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return item is HandyListBoxItem;
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            HandyListBoxItem item = new HandyListBoxItem();
            if (this.ItemContainerStyle != null)
            {
                item.Style = this.ItemContainerStyle;
            }
            return item;
        }

    }
}

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