// <copyright file="TimeControl.xaml.cs" company="GoalBook">
// Copyright © 2009 Mark Brownsword. All rights reserved.
// This source code and supporting files are licensed under The Code Project
// Open License (CPOL) as detailed at http://www.codeproject.com/info/cpol10.aspx.
// </copyright>
namespace GoalBook.Tasks.Controls
{
#region Using Statements
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using GoalBook.Infrastructure;
#endregion
/// <summary>
/// Interaction logic for TimeControl.xaml
/// </summary>
public partial class TimeControl : UserControl
{
#region Constants and Enums
#endregion
#region Instance and Shared Fields
/// <summary>
/// Declaration for SelectedTimeProperty.
/// </summary>
public static readonly DependencyProperty SelectedTimeProperty = DependencyProperty.Register(
"SelectedTime", typeof(TimeInfo?), typeof(TimeControl));
/// <summary>
/// Declaration for SelectedTimeChangedEvent.
/// </summary>
public static readonly RoutedEvent SelectedTimeChangedEvent = EventManager.RegisterRoutedEvent(
"SelectedTimeChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TimeControl));
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the TimeControl class.
/// </summary>
public TimeControl()
{
InitializeComponent();
}
#endregion
#region Delegates and Events
/// <summary>
/// SelectedTimeChanged Event.
/// </summary>
public event RoutedEventHandler SelectedTimeChanged
{
add { AddHandler(SelectedTimeChangedEvent, value); }
remove { RemoveHandler(SelectedTimeChangedEvent, value); }
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the SelectedTime.
/// </summary>
public TimeInfo? SelectedTime
{
get { return (TimeInfo?)GetValue(SelectedTimeProperty); }
set { SetValue(SelectedTimeProperty, value); }
}
#endregion
#region Public and internal Methods
#endregion
#region Base Class Overrides
#endregion
#region Private and Protected Methods
/// <summary>
/// On Selected Time Changed.
/// </summary>
private void OnSelectedTimeChanged()
{
RoutedEventArgs args = new RoutedEventArgs(SelectedTimeChangedEvent);
RaiseEvent(args);
}
/// <summary>
/// Get ComboBox Item.
/// </summary>
/// <param name="value">value parameter</param>
/// <param name="text">text parameter</param>
/// <param name="selectedValue">selectedValue parameter</param>
/// <returns>ComboBoxItem instance containing ComboItemInfo</returns>
private ComboBoxItem GetComboBoxItem(string value, string text, string selectedValue)
{
ComboBoxItem item = new ComboBoxItem();
item.Content = new ComboItemInfo(value, text);
item.IsSelected = value == selectedValue;
return item;
}
#endregion
#region Event Handlers
/// <summary>
/// Handle Control_Loaded event.
/// </summary>
/// <param name="sender">sender parameter</param>
/// <param name="e">RoutedEventArgs parameter</param>
private void Control_Loaded(object sender, RoutedEventArgs e)
{
if (DesignerProperties.GetIsInDesignMode(this))
{
return;
}
// Initialise the ComboBox values.
this.comboBoxHour.Items.Add(this.GetComboBoxItem(null, "N/A", this.SelectedTime.Value.Hour));
this.comboBoxHour.Items.Add(this.GetComboBoxItem("1", "1", this.SelectedTime.Value.Hour));
this.comboBoxHour.Items.Add(this.GetComboBoxItem("2", "2", this.SelectedTime.Value.Hour));
this.comboBoxHour.Items.Add(this.GetComboBoxItem("3", "3", this.SelectedTime.Value.Hour));
this.comboBoxHour.Items.Add(this.GetComboBoxItem("4", "4", this.SelectedTime.Value.Hour));
this.comboBoxHour.Items.Add(this.GetComboBoxItem("5", "5", this.SelectedTime.Value.Hour));
this.comboBoxHour.Items.Add(this.GetComboBoxItem("6", "6", this.SelectedTime.Value.Hour));
this.comboBoxHour.Items.Add(this.GetComboBoxItem("7", "7", this.SelectedTime.Value.Hour));
this.comboBoxHour.Items.Add(this.GetComboBoxItem("8", "8", this.SelectedTime.Value.Hour));
this.comboBoxHour.Items.Add(this.GetComboBoxItem("9", "9", this.SelectedTime.Value.Hour));
this.comboBoxHour.Items.Add(this.GetComboBoxItem("10", "10", this.SelectedTime.Value.Hour));
this.comboBoxHour.Items.Add(this.GetComboBoxItem("11", "11", this.SelectedTime.Value.Hour));
this.comboBoxHour.Items.Add(this.GetComboBoxItem("12", "12", this.SelectedTime.Value.Hour));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem(null, "N/A", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("00", "00", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("01", "01", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("02", "02", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("03", "03", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("04", "04", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("05", "05", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("06", "06", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("07", "07", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("08", "08", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("09", "09", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("10", "10", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("11", "11", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("12", "12", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("13", "13", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("14", "14", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("15", "15", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("16", "16", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("17", "17", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("18", "18", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("19", "19", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("20", "20", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("21", "21", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("22", "22", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("23", "23", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("24", "24", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("25", "25", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("26", "26", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("27", "27", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("28", "28", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("29", "29", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("30", "30", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("31", "31", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("32", "32", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("33", "33", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("34", "34", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("35", "35", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("36", "36", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("37", "37", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("38", "38", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("39", "39", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("40", "40", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("41", "41", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("42", "42", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("43", "43", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("44", "44", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("45", "45", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("46", "46", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("47", "47", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("48", "48", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("49", "49", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("50", "50", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("51", "51", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("52", "52", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("53", "53", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("54", "54", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("55", "55", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("56", "56", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("57", "57", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("58", "58", this.SelectedTime.Value.Minute));
this.comboBoxMinute.Items.Add(this.GetComboBoxItem("59", "59", this.SelectedTime.Value.Minute));
this.comboBoxFormat.Items.Add(this.GetComboBoxItem(null, "N/A", string.IsNullOrEmpty(this.SelectedTime.Value.Format.ToString()) ? null : this.SelectedTime.Value.Format.ToString()));
this.comboBoxFormat.Items.Add(this.GetComboBoxItem("am", "AM", this.SelectedTime.Value.Format.ToString()));
this.comboBoxFormat.Items.Add(this.GetComboBoxItem("pm", "PM", this.SelectedTime.Value.Format.ToString()));
// Initialise the ComboBox event handlers.
this.comboBoxHour.SelectionChanged += this.ComboBox_SelectionChanged;
this.comboBoxMinute.SelectionChanged += this.ComboBox_SelectionChanged;
this.comboBoxFormat.SelectionChanged += this.ComboBox_SelectionChanged;
}
/// <summary>
/// Handle ComboBox_SelectionChanged event.
/// </summary>
/// <param name="sender">sender parameter</param>
/// <param name="e">SelectionChangedEventArgs parameter</param>
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Ensure this SelectionChanged event doesn't bubble up the element tree.
e.Handled = true;
// Initialise the new TwelveHourTimeInfo.
TimeInfo info = new TimeInfo();
info.Hour = ((this.comboBoxHour.SelectedItem as ComboBoxItem).Content as ComboItemInfo).Value;
info.Minute = ((this.comboBoxMinute.SelectedItem as ComboBoxItem).Content as ComboItemInfo).Value;
if (((this.comboBoxFormat.SelectedItem as ComboBoxItem).Content as ComboItemInfo).Value != null)
{
info.Format = (TimeInfo.ClockFormat)Enum.Parse(
typeof(TimeInfo.ClockFormat),
((this.comboBoxFormat.SelectedItem as ComboBoxItem).Content as ComboItemInfo).Value.ToString());
}
// Set SelectedTime (this updates any bound UI elements).
this.SelectedTime = info;
// Raise SelectedTimeChanged event for any listeners.
this.OnSelectedTimeChanged();
}
#endregion
}
}