Click here to Skip to main content
15,885,435 members
Articles / Programming Languages / C#

Build Your Own DataGrid for Silverlight: Step 2

Rate me:
Please Sign up or sign in to vote.
4.39/5 (9 votes)
3 Jun 2009CPOL59 min read 46.5K   854   26  
Implementation of editing and validation features.
// 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 Netika.Windows.Controls;

namespace Open.Windows.Controls
{
    /// <summary>
    /// HandyCommand controls are mainly used to perform actions inside the application. Menus and Toolbars are part of this set. 
    /// </summary>
    public class HandyCommand : HandyListControl
    {
        public static readonly DependencyProperty HandyStyleProperty;
        public static readonly DependencyProperty HandyDefaultItemStyleProperty;
        public static readonly DependencyProperty HandyItemContainerStyleProperty;

        public static readonly DependencyProperty SeparatorStyleProperty;
        public static readonly DependencyProperty StandardItemStyleProperty;
        public static readonly DependencyProperty CheckBoxStyleProperty;
        public static readonly DependencyProperty RadioButtonStyleProperty;
        public static readonly DependencyProperty ItemContainerDefinedStyleProperty;
        public static readonly DependencyProperty NodeStyleProperty;
        public static readonly DependencyProperty DropDownListStyleProperty;
        public static readonly DependencyProperty DropDownButtonStyleProperty;

        static HandyCommand()
        {
            HandyStyleProperty = DependencyProperty.Register("HandyStyle", typeof(HandyCommandStyle), typeof(HandyCommand), null);
            HandyDefaultItemStyleProperty = DependencyProperty.Register("HandyDefaultItemStyle", typeof(HandyCommandItemStyle), typeof(HandyCommand), new PropertyMetadata(new PropertyChangedCallback(OnHandyDefaultItemStyleChanged)));
            HandyItemContainerStyleProperty = DependencyProperty.Register("HandyItemContainerStyle", typeof(HandyCommandItemStyle), typeof(HandyCommand), new PropertyMetadata(new PropertyChangedCallback(OnHandyItemContainerStyleChanged)));

            SeparatorStyleProperty = DependencyProperty.Register("SeparatorStyle", typeof(Style), typeof(HandyCommand), new PropertyMetadata(new PropertyChangedCallback(OnDefinedItemStyleChanged)));
            StandardItemStyleProperty = DependencyProperty.Register("StandardItemStyle", typeof(Style), typeof(HandyCommand), new PropertyMetadata(new PropertyChangedCallback(OnDefinedItemStyleChanged)));
            CheckBoxStyleProperty = DependencyProperty.Register("CheckBoxStyle", typeof(Style), typeof(HandyCommand), new PropertyMetadata(new PropertyChangedCallback(OnDefinedItemStyleChanged)));
            RadioButtonStyleProperty = DependencyProperty.Register("RadioButtonStyle", typeof(Style), typeof(HandyCommand), new PropertyMetadata(new PropertyChangedCallback(OnDefinedItemStyleChanged)));
            NodeStyleProperty = DependencyProperty.Register("NodeStyle", typeof(Style), typeof(HandyCommand), new PropertyMetadata(new PropertyChangedCallback(OnDefinedItemStyleChanged)));
            DropDownListStyleProperty = DependencyProperty.Register("DropDownListStyle", typeof(Style), typeof(HandyCommand), new PropertyMetadata(new PropertyChangedCallback(OnDefinedItemStyleChanged)));
            DropDownButtonStyleProperty = DependencyProperty.Register("DropDownButtonStyle", typeof(Style), typeof(HandyCommand), new PropertyMetadata(new PropertyChangedCallback(OnDefinedItemStyleChanged)));
            ItemContainerDefinedStyleProperty = DependencyProperty.Register("ItemContainerDefinedStyle", typeof(Style), typeof(HandyCommand), new PropertyMetadata(new PropertyChangedCallback(OnDefinedItemStyleChanged)));
                       
        }

        public HandyCommand()
        {
            this.DefaultStyleKey = typeof(HandyCommand);
           
        }
                
        /// <summary>
        /// The HandyCommandItemStyle that is applied to the itemcontainer items of the HandyCommand control  
        /// </summary>
        public HandyCommandItemStyle HandyItemContainerStyle
        {
            get
            {
                return (HandyCommandItemStyle)this.GetValue(HandyItemContainerStyleProperty);
            }
            set
            {
                this.SetValue(HandyItemContainerStyleProperty, value);
            }
        }

        private static void OnHandyItemContainerStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandyCommand containerControl = (HandyCommand)d;

            containerControl.ApplyItemContainerStyle(containerControl.GetHandyItemContainerStyle());

        }

        private void ApplyItemContainerStyle(Style handyItemContainerStyle)
        {
            if (handyItemContainerStyle == null)
                return;

            if (this.ItemContainerStyle == null)
                this.ItemContainerStyle = handyItemContainerStyle;

        }

        private Style GetHandyItemContainerStyle()
        {
            if (HandyItemContainerStyle == HandyCommandItemStyle.None)
                return null;

            return GetItemStyle(HandyItemContainerStyle);

        }

        /// <summary>
        /// The default HandyCommandItemStyle that is applied to the CommandItems of the HandyCommand control  
        /// </summary>
        public HandyCommandItemStyle HandyDefaultItemStyle
        {
            get
            {
                return (HandyCommandItemStyle)this.GetValue(HandyDefaultItemStyleProperty);
            }
            set
            {
                base.SetValue(HandyDefaultItemStyleProperty, value);
            }
        }

        private static void OnHandyDefaultItemStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandyCommand containerControl = (HandyCommand)d;

            containerControl.ApplyDefaultItemStyle(containerControl.GetHandyDefaultItemStyle());
        }

        private void ApplyDefaultItemStyle(Style handyDefaultItemStyle)
        {
            if (handyDefaultItemStyle == null)
                return;

            if (this.DefaultItemStyle == null)
                this.DefaultItemStyle = handyDefaultItemStyle;

        }

        private Style GetHandyDefaultItemStyle()
        {
            HandyCommandItemStyle handyDefaultItemStyle = HandyDefaultItemStyle;

            if (handyDefaultItemStyle == HandyCommandItemStyle.Calculated)
                handyDefaultItemStyle = GetCalculatedHandyDefaultItemStyle();
            
            if (handyDefaultItemStyle == HandyCommandItemStyle.None)
                return null;

            return GetItemStyle(handyDefaultItemStyle);

        }

        private HandyCommandItemStyle GetCalculatedHandyDefaultItemStyle()
        {
            switch (SelectionMode)
            {
                case SelectionMode.Multiple:
                    return HandyCommandItemStyle.CheckBox;

                case SelectionMode.Option:
                    return HandyCommandItemStyle.RadioButton;

                default:
                    return HandyCommandItemStyle.StandardItem;

            }
        }

        private static void OnDefinedItemStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandyCommand containerControl = (HandyCommand)d;

            if (containerControl.DefaultItemStyle == null)
                containerControl.ApplyDefaultItemStyle(containerControl.GetHandyDefaultItemStyle());

            if (containerControl.ItemContainerStyle == null)
                containerControl.ApplyItemContainerStyle(containerControl.GetHandyItemContainerStyle());
        }

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

        protected override Style FindHandyStyle()
        {
            if (HandyStyle != HandyCommandStyle.None)
                return ResourceHelper.FindResource(Enum.GetName(typeof(HandyCommandStyle), HandyStyle)) as Style;
            else
                return ResourceHelper.FindResource(Enum.GetName(typeof(HandyCommandStyle), HandyCommandStyle.ToolbarStyle)) as Style;
        }
       
        public Style GetItemStyle(HandyCommandItemStyle handyCommandItemStyle)
        {
            switch (handyCommandItemStyle)
            {
                case HandyCommandItemStyle.CheckBox:
                    return this.CheckBoxStyle;

                case HandyCommandItemStyle.ItemContainer:
                    return this.ItemContainerDefinedStyle;

                case HandyCommandItemStyle.Node:
                    return this.NodeStyle;

                case HandyCommandItemStyle.DropDownList:
                    return this.DropDownListStyle;

                case HandyCommandItemStyle.DropDownButton:
                    return this.DropDownButtonStyle;

                case HandyCommandItemStyle.RadioButton:
                    return this.RadioButtonStyle;

                case HandyCommandItemStyle.Separator:
                    return this.SeparatorStyle;

                case HandyCommandItemStyle.StandardItem:
                    return this.StandardItemStyle;
               
            }

            return null;
        }        

        /// <summary>
        /// The (real) Style to apply to a CommandItem to which the HandyCommandItemStyle.Separator HandyStyle is applied.  
        /// </summary>
        public Style SeparatorStyle
        {
            get
            {
                return (Style)this.GetValue(SeparatorStyleProperty);
            }
            set
            {
                base.SetValue(SeparatorStyleProperty, value);
            }
        }
        
        /// <summary>
        /// The (real) style to apply to a CommandItem to which the HandyCommandItemStyle.StandardItem HandyStyle is applied.  
        /// </summary>
        public Style StandardItemStyle
        {
            get
            {
                return (Style)this.GetValue(StandardItemStyleProperty);
            }
            set
            {
                base.SetValue(StandardItemStyleProperty, value);
            }
        }

        /// <summary>
        /// The (real) style to apply to a CommandItem to which the HandyCommandItemStyle.CheckBox HandyStyle is applied.  
        /// </summary>
        public Style CheckBoxStyle
        {
            get
            {
                return (Style)this.GetValue(CheckBoxStyleProperty);
            }
            set
            {
                base.SetValue(CheckBoxStyleProperty, value);
            }
        }

        /// <summary>
        /// The (real) style to apply to a CommandItem to which the HandyCommandItemStyle.RadioButton HandyStyle is applied.  
        /// </summary>
        public Style RadioButtonStyle
        {
            get
            {
                return (Style)this.GetValue(RadioButtonStyleProperty);
            }
            set
            {
                base.SetValue(RadioButtonStyleProperty, value);
            }
        }

        /// <summary>
        /// The (real) style to apply to a CommandItem to which the HandyCommandItemStyle.Node HandyStyle is applied.  
        /// </summary>
        public Style NodeStyle
        {
            get
            {
                return (Style)this.GetValue(NodeStyleProperty);
            }
            set
            {
                base.SetValue(NodeStyleProperty, value);
            }
        }

        /// <summary>
        /// The (real) style to apply to a CommandItem to which the HandyCommandItemStyle.DropDownList HandyStyle is applied. 
        /// </summary>
        public Style DropDownListStyle
        {
            get
            {
                return (Style)this.GetValue(DropDownListStyleProperty);
            }
            set
            {
                base.SetValue(DropDownListStyleProperty, value);
            }
        }

        /// <summary>
        /// The (real) style to apply to a CommandItem to which the HandyCommandItemStyle.DropDownButton HandyStyle is applied.  
        /// </summary>
        public Style DropDownButtonStyle
        {
            get
            {
                return (Style)this.GetValue(DropDownButtonStyleProperty);
            }
            set
            {
                base.SetValue(DropDownButtonStyleProperty, value);
            }
        }

        /// <summary>
        /// The (real) style to apply to a CommandItem to which the HandyCommandItemStyle.ItemContainer HandyStyle is applied.  
        /// </summary>
        public Style ItemContainerDefinedStyle
        {
            get
            {
                return (Style)this.GetValue(ItemContainerDefinedStyleProperty);
            }
            set
            {
                base.SetValue(ItemContainerDefinedStyleProperty, value);
            }
        }

        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            CommandItem commandItem = element as CommandItem;
            if (commandItem != null)
            {
                ItemSource itemSource = item as ItemSource;
                if (itemSource != null)
                {
                    CommandDataItem commandDataItem = itemSource.DataContext as CommandDataItem;
                    if ((commandDataItem != null))
                    {
                        if (commandDataItem.HandyStyle != HandyCommandItemStyle.None)
                            commandItem.HandyStyle = commandDataItem.HandyStyle;

                        if (commandDataItem.HandyOverflowedStyle != HandyCommandItemStyle.None)
                            commandItem.HandyOverflowedStyle = commandDataItem.HandyOverflowedStyle;


                    }
                }
            }
            
            base.PrepareContainerForItemOverride(element, item);

            
        }
        protected override DependencyObject GetDefaultItem()
        {
            return new CommandItem();
        }

        protected override bool IsDefaultItem(object item)
        {
            return (item is CommandItem);
        }

        protected override Netika.Windows.Controls.Item GetDefaultOverflowedItem()
        {
            return new CommandItem();
        }

        protected override ListControl GetNewContentListControl()
        {
            return new HandyCommand();
        }
    }
}

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