Click here to Skip to main content
15,878,748 members
Articles / Desktop Programming / WPF

A C# WPF .NET 4.0 "DataGrid" with Persistent Controls in Cells

Rate me:
Please Sign up or sign in to vote.
4.29/5 (10 votes)
29 Aug 2011CPOL10 min read 47.5K   2.4K   28  
A DataGrid lookalike that has persistent controls in cells
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PersistDataGrid
{
    /// <summary>
    /// Interaction logic for Header.xaml
    /// </summary>

    public enum SortType : byte
    {
        Ascending,
        Descending,
        NotOrdered
    }

    public delegate SortType HeaderClickedDelegate(Header header, SortType SortType);

    public partial class Header : UserControl
    {
        private HeaderClickedDelegate HeadClickDel;
            
        private SortType CurrentSortType = SortType.NotOrdered;
 
        public Header(string title, HeaderClickedDelegate HeadClickDel)
        {
            InitializeComponent();
            Title.Content = title;
            this.HeadClickDel = HeadClickDel;
        }
        private PathGeometry GetArrowDrawing(bool Up)
        {
            PathFigure ArrowFigure = new PathFigure();
            ArrowFigure.StartPoint = (Up) ? new Point(0, 5) : new Point(0.0, 1.0);

            List<Point> PointList = new List<Point>();

            if (Up)
            {
                PointList.Add(new Point(3.5, 1.0));
                PointList.Add(new Point(7.0, 5.0));
            }
            else
            {
                PointList.Add(new Point(3.5, 5.0));
                PointList.Add(new Point(7.0, 1.0));
            }

            ArrowFigure.Segments.Add(new PolyLineSegment(PointList, true));

            PathGeometry ArrowGeometry = new PathGeometry();
            ArrowGeometry.Figures.Add(ArrowFigure);

            return ArrowGeometry;
        }
        private void UpdateSortArrow()
        {
            switch (CurrentSortType)
            {
                case SortType.NotOrdered:
                    Arrow.Data = null;
                    break;
                case SortType.Ascending:
                    Arrow.Data = GetArrowDrawing(true);
                    break;
                case SortType.Descending:
                    Arrow.Data = GetArrowDrawing(false);
                    break;
            }
        }

        private void UserControl_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            CurrentSortType = HeadClickDel(this, CurrentSortType);
            UpdateSortArrow();
        }

        public void ResetSortType()
        {
            if (CurrentSortType != SortType.NotOrdered)
            {
                CurrentSortType = SortType.NotOrdered;
                UpdateSortArrow();
            }
        }

        private void UserControl_GotMouseCapture(object sender, MouseEventArgs e)
        {
            HeaderBorder.CaptureMouse();
        }
    }
}

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
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions