Click here to Skip to main content
15,893,487 members
Articles / Desktop Programming / WPF

WPF: A TimeLineControl

Rate me:
Please Sign up or sign in to vote.
4.97/5 (162 votes)
14 Apr 2010CPOL19 min read 350.6K   5.5K   282  
Simple WPF TimeLineControl That I Think May Be Useful
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;
using System.Collections.ObjectModel;

namespace TimeLineControl
{
    /// <summary>
    /// Displays decade of <c>ITemLineItem</c>s
    /// </summary>
    public partial class DecadeView : UserControl
    {
        #region Data
        private Int32 decade;
        private Boolean initialised = false;
        private List<Int32> yearsForDecade = null;
        #endregion

        #region Ctor
        public DecadeView()
        {
            InitializeComponent();
        }
        #endregion

        #region Public Properties

        public Boolean ShouldStretchToFit { private get; set; }

        public Int32 DecadeText
        {
            set
            {
                decade = value;
                yearsForDecade = DataHelper.GetDecadesYearsForDecade(decade);
                btnDecadeText.Content = String.Format("{0} - {1}",
                    yearsForDecade[0], yearsForDecade[9]);
            }
        }


        #region ItemsDataTemplate

        /// <summary>
        /// ItemsDataTemplate Dependency Property
        /// </summary>
        public static readonly DependencyProperty ItemsDataTemplateProperty =
            DependencyProperty.Register("ItemsDataTemplate", typeof(DataTemplate),
            typeof(DecadeView),
                new FrameworkPropertyMetadata((DataTemplate)null,
                    new PropertyChangedCallback(OnItemsDataTemplateChanged)));

        /// <summary>
        /// Gets or sets the ItemsDataTemplate property.  
        /// </summary>
        public DataTemplate ItemsDataTemplate
        {
            get { return (DataTemplate)GetValue(ItemsDataTemplateProperty); }
            set { SetValue(ItemsDataTemplateProperty, value); }
        }

        /// <summary>
        /// Handles changes to the ItemsDataTemplate property.
        /// </summary>
        private static void OnItemsDataTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }
        #endregion

        #region TimeLineItems

        /// <summary>
        /// TimeLineItems Dependency Property
        /// </summary>
        public static readonly DependencyProperty TimeLineItemsProperty =
            DependencyProperty.Register("TimeLineItems", typeof(List<ITimeLineItem>),
            typeof(DecadeView),
                new FrameworkPropertyMetadata((List<ITimeLineItem>)null,
                    new PropertyChangedCallback(OnTimeLineItemsChanged)));

        /// <summary>
        /// Gets or sets the TimeLineItems property.  
        /// </summary>
        public List<ITimeLineItem> TimeLineItems
        {
            get { return (List<ITimeLineItem>)GetValue(TimeLineItemsProperty); }
            set { SetValue(TimeLineItemsProperty, value); }
        }

        /// <summary>
        /// Handles changes to the TimeLineItems property.
        /// </summary>
        private static void OnTimeLineItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {


        }
        #endregion

        #endregion

        #region Public Methods
        public void Draw()
        {
            if (TimeLineItems.Count() == 0)
                return;

            if (initialised)
                return;

            grid.Children.Clear();
            Int32 barHeightToDrawIn = (Int32)this.Height - 35;


            Int32 maximumItemsForGraphAcrossAllBars = 0;
            Dictionary<Int32, List<ITimeLineItem>> itemsByKey = 
                new Dictionary<int, List<ITimeLineItem>>();

            Double barWidth = 15.0;
            if (this.ShouldStretchToFit)
                barWidth = this.ActualWidth / 10;

            foreach (ColumnDefinition col in grid.ColumnDefinitions)
            {
                col.Width = new GridLength(barWidth, GridUnitType.Pixel);
            }


            for (int i = 0; i < 10; i++)
            {
                List<ITimeLineItem> items =
                 (from t in TimeLineItems 
                  where t.ItemTime.Year == yearsForDecade[i] 
                  select t).ToList();
                itemsByKey.Add(yearsForDecade[i], items);
            }


            if (itemsByKey.Count > 0)
                maximumItemsForGraphAcrossAllBars = 
                    (from x in itemsByKey 
                     select x.Value.Count).Max();

            for (int i = 0; i < 10; i++)
            {
                Int32 checkingForYear = yearsForDecade[i];
                List<ITimeLineItem> items = null;

                if (itemsByKey.TryGetValue(checkingForYear, out items))
                {

                    ItemsBar bar = new ItemsBar();
                    bar.ItemsDataTemplate = this.ItemsDataTemplate;
                    bar.Height = barHeightToDrawIn;
                    bar.Width = grid.ColumnDefinitions[0].Width.Value;
                    bar.TimeLineItems = items;
                    bar.MaximumItemsForGraphAcrossAllBars = maximumItemsForGraphAcrossAllBars;
                    bar.Loaded += (s, e) =>
                        {
                            bar.Draw();
                        };
                    bar.SetValue(Grid.ColumnProperty, i);
                    bar.SetValue(Grid.RowProperty, 0);
                    grid.Children.Add(bar);
                }


            }

            initialised = true;
        }
        #endregion

        #region Events
        public event EventHandler<DateEventArgs> ViewDateEvent;
        #endregion

        #region Private Methods
        private void OnViewDateEvent(DateTime currentViewingDate)
        {
            // Copy to a temporary variable to be thread-safe.
            EventHandler<DateEventArgs> temp = ViewDateEvent;
            if (temp != null)
                temp(this, new DateEventArgs(currentViewingDate));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OnViewDateEvent(new DateTime(yearsForDecade[9], 1, 1));
        }
        #endregion
    }
}

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
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions