Click here to Skip to main content
15,885,141 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 348.2K   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 year of <c>ITemLineItem</c>s
    /// </summary>
    public partial class YearView : UserControl
    {
        #region Data
        Dictionary<Int32, List<ITimeLineItem>> itemsByKey = new Dictionary<int, List<ITimeLineItem>>();
        private Boolean initialised = false;
        #endregion

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

        #region Public Properties

        public Int32 StartYear { get; set; }

        #region ItemsDataTemplate

        /// <summary>
        /// ItemsDataTemplate Dependency Property
        /// </summary>
        public static readonly DependencyProperty ItemsDataTemplateProperty =
            DependencyProperty.Register("ItemsDataTemplate", typeof(DataTemplate),
            typeof(YearView),
                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(YearView),
                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;

            Double barWidth = (this.ActualWidth / 10) > 50.0 ? this.ActualWidth / 10 : 50.0;
            foreach (ColumnDefinition col in grid.ColumnDefinitions)
            {
                col.Width = new GridLength(barWidth,GridUnitType.Pixel);
            }

            Int32 maximumItemsForGraphAcrossAllBars = 0;
            

            for (int i = 0; i < 10; i++)
            {
                Int32 checkingForYear = StartYear+i;
                List<ITimeLineItem> items =
                 (from t in TimeLineItems 
                  where t.ItemTime.Year == checkingForYear 
                  select t).ToList();

                if (items != null && items.Count > 0)
                    itemsByKey.Add(checkingForYear, items);

            }

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

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


                Double columnWidth = grid.ActualWidth / 10; 

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

                    ItemsBar bar = new ItemsBar();
                    bar.ItemsDataTemplate = this.ItemsDataTemplate;
                    bar.Height = barHeightToDrawIn;
                    bar.Width = columnWidth;
                    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);
                }

                //Need to build up buttons now
                grid.Children.Add(CreateButton(checkingForYear, i));
            }

            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 Button CreateButton(Int32 year, Int32 column)
        {
            Button btn = new Button();
            btn.Content = year;
            Style style = (Style)this.Resources["graphSectionButtonStyle"];
            if (style != null) { btn.Style = style; }
            btn.Click += Button_Click;
            btn.IsEnabled = itemsByKey.ContainsKey(year);
            btn.SetValue(Grid.RowProperty, 1);
            btn.SetValue(Grid.ColumnProperty, column);
            return btn;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            OnViewDateEvent(new DateTime(Int32.Parse(btn.Content.ToString()), 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