Click here to Skip to main content
15,893,564 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.

// 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.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System;

namespace Open.Windows.Controls
{
    public class TextCell : Cell
    {
        public static readonly DependencyProperty TextProperty;

        static TextCell()
        {
            TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(TextCell), null);
        }

        public TextCell()
        {
            DefaultStyleKey = typeof(TextCell);
        }

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        private Panel textContainerElement;
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            textContainerElement = GetTemplateChild("TextContainerElement") as Panel;
        }

        private void textBoxElement_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.CellState == CellState.Edited)
                this.IsDirty = true;
        }

        private CellTextBox textBoxElement;
        protected override bool OnBeginEdit()
        {
            if (textContainerElement != null)
            {
                if (textBoxElement == null)
                {
                    textBoxElement = new CellTextBox();
                    textBoxElement.Style = ResourceHelper.FindResource("CellTextBoxStyle") as Style;
                    textBoxElement.Text = this.Text;
                    textBoxElement.Margin = this.Padding;
                    textBoxElement.HorizontalAlignment = this.HorizontalContentAlignment;
                    textBoxElement.VerticalAlignment = this.VerticalContentAlignment;
                    textBoxElement.Foreground = this.Foreground;
                    textBoxElement.TextChanged += new TextChangedEventHandler(textBoxElement_TextChanged);

                    textContainerElement.Children.Add(textBoxElement);
                }

                if (textBoxElement.Focus())
                {
                    textBoxElement.SelectionStart = textBoxElement.Text.Length;
                    return true;
                }
            }

            return false;
        }

        private long lastTick;
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            if (this.CellState == CellState.Focused)
                BeginEdit();

            if (this.CellState == CellState.Edited)
            {
                e.Handled = true; //we do not want that the ContainerItem OnMouseLeftButtonDown is called

                if (DateTime.Now.Ticks - lastTick < 2000000) //It is a double click
                    DoubleClick();
            }

            lastTick = DateTime.Now.Ticks;
            if (textBoxElement != null)
                textBoxElement.LastTick = lastTick;

        }

        private void DoubleClick()
        {
            if (textBoxElement != null)
                textBoxElement.SelectAll();
        }

        protected override void OnCommitEdit()
        {
            if (textBoxElement != null)
                this.Text = textBoxElement.Text;
        }

        protected override void OnCancelEdit()
        {
            if (textBoxElement != null)
                textBoxElement.Text = this.Text;
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (CellState != CellState.Edited)
            {
                switch (e.Key)
                {
                    case Key.Left:
                    case Key.Up:
                    case Key.Down:
                    case Key.Right:
                    case Key.PageDown:
                    case Key.PageUp:
                    case Key.Home:
                    case Key.End:
                    case Key.Enter:
                    case Key.Ctrl:
                    case Key.Shift:
                    case Key.Alt:
                    case Key.Escape:
                    case Key.Tab:
                        break;

                    default:
                        if (BeginEdit() && (textBoxElement != null))
                            textBoxElement.Text = "";
                        break;
                }
            }

            base.OnKeyDown(e);
        }

        protected override object CurrentValue
        {
            get { return this.Text; }
        }

        protected override object DirtyValue
        {
            get
            {
                if (textBoxElement != null)
                    return textBoxElement.Text;

                return this.Text;
            }
        }

        protected override void AfterEdit()
        {
            if ((textBoxElement != null) && (textContainerElement != null)) 
            {
                if (textBoxElement == FocusManager.GetFocusedElement())
                    this.Focus();

                textContainerElement.Children.Remove(textBoxElement);
                textBoxElement.TextChanged -= new TextChangedEventHandler(textBoxElement_TextChanged);

                textBoxElement = null;
            }
        }
    }
}

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