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

namespace Open.Windows.Controls
{
    /// <summary>
    /// Base control from which the HandyCommand, HandyContainer and HandyNavigator controls are inherited. 
    /// </summary>
    public abstract class HandyListControl: Netika.Windows.Controls.Combo
    {
        public static readonly DependencyProperty HandyScrollerStyleProperty;
        public static readonly DependencyProperty StyleBindingModeProperty;
        public static readonly DependencyProperty HandyItemsPanelModelProperty;
        public static readonly DependencyProperty HandyStatersModelProperty;

        
        static HandyListControl()
        {
            HandyScrollerStyleProperty = DependencyProperty.Register("HandyScrollerStyle", typeof(HandyScrollerStyle), typeof(HandyListControl), new PropertyMetadata(new PropertyChangedCallback(OnHandyScrollerStyleChanged)));
            StyleBindingModeProperty = DependencyProperty.Register("StyleBindingMode", typeof(StyleBindingMode), typeof(HandyListControl), new PropertyMetadata(StyleBindingMode.Late));

            HandyItemsPanelModelProperty = DependencyProperty.Register("HandyItemsPanelModel", typeof(HandyPanelModel), typeof(HandyListControl), new PropertyMetadata(new PropertyChangedCallback(OnHandyItemsPanelModelChanged)));
            HandyStatersModelProperty = DependencyProperty.Register("HandyStatersModel", typeof(HandyStaters), typeof(HandyListControl), new PropertyMetadata(new PropertyChangedCallback(OnHandyStatersModelChanged)));
            
        }

        protected HandyListControl()
        {
            this.LayoutUpdated += new EventHandler(HandyList_LayoutUpdated);
        }

        private void HandyList_LayoutUpdated(object sender, EventArgs e)
        {            
            if (!handyStyleApplied)
            {
                if (StyleBindingMode == StyleBindingMode.Normal)
                    this.ApplyHandyStyle(this.FindHandyStyle());
                else if ((this.ActualHeight > 0) || (this.ActualWidth > 0))
                    this.ApplyHandyStyle(this.FindHandyStyle());
            }
        }

        protected override void InitializeStaters()
        {
            if (this.Style != null)
                base.InitializeStaters();
        }

        protected abstract Style FindHandyStyle();

        /// <summary>
        /// Allows to define when the HandyStyle is applied to a control. 
        /// </summary>
        public StyleBindingMode StyleBindingMode
        {
            get
            {
                return (StyleBindingMode)this.GetValue(StyleBindingModeProperty);
            }
            set
            {
                base.SetValue(StyleBindingModeProperty, value);
            }
        }

        /// <summary>
        /// Allows to select the StatersModel (collection of Stater) that will be used in the HandyContainer control.
        /// </summary>
        public HandyStaters HandyStatersModel
        {
            get
            {
                return (HandyStaters)this.GetValue(HandyStatersModelProperty);
            }
            set
            {
                base.SetValue(HandyStatersModelProperty, value);
            }
        }

        private static void OnHandyStatersModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandyListControl listControl = (HandyListControl)d;

            listControl.ApplyStatersModel(listControl.GetHandyStatersModel());
        }

        private void ApplyStatersModel(Staters handyStatersModel)
        {
            if (handyStatersModel == null)
                return;

            if (this.StatersModel == null)
                this.StatersModel = handyStatersModel;

        }

        private Staters GetHandyStatersModel()
        {
            if (HandyStatersModel == HandyStaters.None)
                return null;

            return ResourceHelper.FindResource(Enum.GetName(typeof(HandyStaters), HandyStatersModel)) as Staters;

        }

        /// <summary>
        /// Allows to select the PanelModel (not only a Panel but also a value for its properties including the KeyNavigator and the ChildrenAnimator) that will contain the items of the HandyContainer control (ItemsHost)
        /// </summary>
        public HandyPanelModel HandyItemsPanelModel
        {
            get
            {
                return (HandyPanelModel)this.GetValue(HandyItemsPanelModelProperty);
            }
            set
            {
                base.SetValue(HandyItemsPanelModelProperty, value);
            }
        }

        private static void OnHandyItemsPanelModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandyListControl listControl = (HandyListControl)d;

            listControl.ApplyItemsPanelModel(listControl.GetHandyItemsPanelModel());
        }

        private void ApplyItemsPanelModel(PanelModel handyItemPanelModel)
        {
            if (handyItemPanelModel == null)
                return;

            if (this.ItemsPanelModel == null)
                this.ItemsPanelModel = handyItemPanelModel;

        }

        private PanelModel GetHandyItemsPanelModel()
        {
            if (HandyItemsPanelModel == HandyPanelModel.None)
                return null;

            return ResourceHelper.FindResource(Enum.GetName(typeof(HandyPanelModel), HandyItemsPanelModel)) as PanelModel;
        }
               
        /// <summary>
        /// The HandyScrollerStyle to apply to the Scroller (if any) that is inside the ListControl. 
        /// </summary>
        public HandyScrollerStyle HandyScrollerStyle
        {
            get
            {
                return (HandyScrollerStyle)this.GetValue(HandyScrollerStyleProperty);
            }
            set
            {
                base.SetValue(HandyScrollerStyleProperty, value);
            }
        }

        private static void OnHandyScrollerStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandyListControl listControl = (HandyListControl)d;

            listControl.ApplyScrollerStyle(listControl.GetHandyScrollerStyle());
        }

        private void ApplyScrollerStyle(Style handyScrollerStyle)
        {
            if (handyScrollerStyle == null)
                return;
            
            if (this.ScrollerStyle == null)
                this.ScrollerStyle = handyScrollerStyle;
        }

        private Style GetHandyScrollerStyle()
        {
            HandyScrollerStyle handyScrollerStyle = HandyScrollerStyle;

            if (handyScrollerStyle == HandyScrollerStyle.Calculated)
                handyScrollerStyle = GetCalculatedHandyScrollerStyle();

            if (handyScrollerStyle == HandyScrollerStyle.None)
                return null;

            return ResourceHelper.FindResource(Enum.GetName(typeof(HandyScrollerStyle), handyScrollerStyle)) as Style;
        }

        private HandyScrollerStyle GetCalculatedHandyScrollerStyle()
        {
            if (OverflowControl != null)
                return HandyScrollerStyle.NoScrollStyle;

            return HandyScrollerStyle.SmallScrollerStyle;
        }

        /// <summary>
        /// This event occurs when the real style linked to the HandyStyle has been applied to the control. 
        /// </summary>
        public event EventHandler HandyStyleApplied;               
        protected virtual void OnHandyStyleApplied(EventArgs e)
        {
            if (HandyStyleApplied != null)
                HandyStyleApplied(this, e);
        }

        private bool onHandyStyleAppliedEventCalled;
        private bool templateLoaded;
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (handyStyleApplied)
            {
                if (!onHandyStyleAppliedEventCalled)
                {
                    OnHandyStyleApplied(EventArgs.Empty);
                    onHandyStyleAppliedEventCalled = true;
                }
            }

            templateLoaded = true;
           
        }

        private bool handyStyleApplied;
        protected void ApplyHandyStyle(Style style)
        {
            if (style == null)
                return;

            if (this.Style == null)
            {
                this.Style = style;
                handyStyleApplied = true;

                if (templateLoaded && !onHandyStyleAppliedEventCalled)
                    OnHandyStyleApplied(EventArgs.Empty);
            }

        }

        /// <summary>
        /// Gets an item from its index
        /// </summary>
        public HandyListItem GetItemFromIndex(int itemIndex)
        {
            if (this.VirtualMode == VirtualMode.On)
                itemIndex = itemIndex - this.VirtualPageStartIndex;

            if (itemIndex < 0)
                return null;

            UIElementCollection itemsHostChildren = this.ItemsHost.Children;
            if (itemIndex >= itemsHostChildren.Count)
                return null;

            return (HandyListItem)itemsHostChildren[itemIndex];

        }

        /// <summary>
        /// Ensure that an item is located inside the display area of the control
        /// </summary>
        public void EnsureItemVisible(int itemIndex)
        {
            GStackPanel itemsHost = (GStackPanel)this.ItemsHost;
            if (itemIndex < this.VerticalOffset)
            {
                this.VerticalOffset = itemIndex;
            }
            if (itemIndex > this.VerticalOffset + this.ViewportHeight)
            {
                this.VerticalOffset = (int)(itemIndex - this.ViewportHeight);
                if (this.VirtualMode == VirtualMode.On)
                    itemsHost.EnsureVisible(itemIndex - this.VirtualPageStartIndex, EnsureVisibleLocation.Bottom);
                else
                    itemsHost.EnsureVisible(itemIndex, EnsureVisibleLocation.Bottom);
            }
        }
    }
}

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