Click here to Skip to main content
15,887,214 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;

namespace Open.Windows.Controls
{
    /// <summary>
    /// Content control that the user can resize. The WindowSizer control forces the size of its content according to the user wishes.
    /// </summary>
    public class HandyWindowSizer : Netika.Windows.Controls.WindowSizer
    {
        public static readonly DependencyProperty HandyStyleProperty;

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

        public HandyWindowSizer()
        {
            DefaultStyleKey = typeof(HandyWindowSizer);
            this.LayoutUpdated += new EventHandler(HandyWindowSizer_LayoutUpdated);
        }

        private void HandyWindowSizer_LayoutUpdated(object sender, EventArgs e)
        {
            if (!handyStyleApplied)
            {
                this.ApplyHandyStyle();
            }
        }
        
        /// <summary>
        /// The Style to apply to the control  
        /// </summary>
        public HandyWindowSizerStyle HandyStyle
        {
            get
            {
                return (HandyWindowSizerStyle)this.GetValue(HandyStyleProperty);
            }
            set
            {
                base.SetValue(HandyStyleProperty, value);
            }
        }
       

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

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

        /// <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;

        }
    }
}

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