using BPA.UIControl.Commons.KnownBoxes;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace BPA.UIControl
{
///
/// 时钟
///
[TemplatePart(Name = HourListPartName, Type = typeof(Selector))]
[TemplatePart(Name = MinuteListPartName, Type = typeof(Selector))]
[TemplatePart(Name = ConfirmPartName, Type = typeof(Button))]
public class Clock : Control
{
///
/// 小时列表名称
///
public const string HourListPartName = "PART_HourList";
///
/// 分钟列表名称
///
public const string MinuteListPartName = "PART_MinuteList";
///
/// 确认按钮名称
///
public const string ConfirmPartName = "PART_ConfirmButton";
///
/// Initializes a new instance of the class.
///
static Clock()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Clock), new FrameworkPropertyMetadata(typeof(Clock)));
}
///
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (GetTemplateChild(ConfirmPartName) is Button confirmButton)
{
confirmButton.Click += ConfirmButton_Click;
}
Hours = new ObservableCollection(Enumerable.Range(0, 24));
Minutes = new ObservableCollection(Enumerable.Range(0, 60));
CurrentTime = new DateTime(DateTime.Now.TimeOfDay.Ticks);
Hour = CurrentTime.Value.Hour;
Minute = CurrentTime.Value.Minute;
}
#region 路由事件
///
/// 选择时间改变事件
///
public static readonly RoutedEvent SelectedTimeChangedEvent = EventManager.RegisterRoutedEvent(
"SelectedTimeChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler), typeof(Clock));
///
/// 选择时间改变事件处理
///
public event RoutedPropertyChangedEventHandler SelectedTimeChanged
{
add { AddHandler(SelectedTimeChangedEvent, value); }
remove { RemoveHandler(SelectedTimeChangedEvent, value); }
}
///
/// 当前事件改变事件
///
public static readonly RoutedEvent CurrentTimeChangedEvent = EventManager.RegisterRoutedEvent(
"CurrentTimeChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler), typeof(Clock));
///
/// 当前事件改变事件处理
///
public event RoutedPropertyChangedEventHandler CurrentTimeChanged
{
add { AddHandler(CurrentTimeChangedEvent, value); }
remove { RemoveHandler(CurrentTimeChangedEvent, value); }
}
#endregion
#region 依赖属性
///
/// 时集合
///
internal static readonly DependencyProperty HoursProperty = DependencyProperty.Register(
"Hours", typeof(ObservableCollection), typeof(Clock), new PropertyMetadata(null));
///
/// 时集合
///
internal ObservableCollection Hours
{
get { return (ObservableCollection)GetValue(HoursProperty); }
set { SetValue(HoursProperty, value); }
}
///
/// 时
///
public static readonly DependencyProperty HourProperty = DependencyProperty.Register(
"Hour", typeof(int), typeof(Clock), new PropertyMetadata(0, OnItemSeletedChanged));
///
/// 时
///
public int Hour
{
get { return (int)GetValue(HourProperty); }
set { SetValue(HourProperty, value); }
}
///
/// 分集合
///
internal static readonly DependencyProperty MinutesProperty = DependencyProperty.Register(
"Minutes", typeof(ObservableCollection), typeof(Clock), new PropertyMetadata(null));
///
/// 分集合
///
internal ObservableCollection Minutes
{
get { return (ObservableCollection)GetValue(MinutesProperty); }
set { SetValue(MinutesProperty, value); }
}
///
/// 分
///
public static readonly DependencyProperty MinuteProperty = DependencyProperty.Register(
"Minute", typeof(int), typeof(Clock), new PropertyMetadata(0, OnItemSeletedChanged));
///
/// 分
///
public int Minute
{
get { return (int)GetValue(MinuteProperty); }
set { SetValue(MinuteProperty, value); }
}
///
/// 选中时间
///
public static readonly DependencyProperty SelectedTimeProperty = DependencyProperty.Register(
"SelectedTime", typeof(DateTime?), typeof(Clock), new PropertyMetadata(default(DateTime), OnSelectTimeChanged));
private static void OnSelectTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var clock = (Clock)d;
if (clock.SelectedTime != null)
{
clock.Hour = clock.SelectedTime.Value.Hour;
clock.Minute = clock.SelectedTime.Value.Minute;
}
}
///
/// 选中时间
///
public DateTime? SelectedTime
{
get { return (DateTime?)GetValue(SelectedTimeProperty); }
set { SetValue(SelectedTimeProperty, value); }
}
///
/// 当前时间
///
public static readonly DependencyProperty CurrentTimeProperty =
DependencyProperty.Register("CurrentTime", typeof(DateTime?), typeof(Clock), new PropertyMetadata(default(DateTime?)));
///
/// 当前时间
///
public DateTime? CurrentTime
{
get { return (DateTime?)GetValue(CurrentTimeProperty); }
set { SetValue(CurrentTimeProperty, value); }
}
///
/// 是否显示确认按钮
///
public static readonly DependencyProperty IsShowConfirmButtonProperty =
DependencyProperty.Register("IsShowConfirmButton", typeof(bool), typeof(Clock), new PropertyMetadata(BooleanBoxes.TrueBox));
///
/// 是否显示确认按钮
///
public bool IsShowConfirmButton
{
get { return (bool)GetValue(IsShowConfirmButtonProperty); }
set { SetValue(IsShowConfirmButtonProperty, BooleanBoxes.Box(value)); }
}
#endregion
///
/// 时间选择改变
///
private static void OnItemSeletedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var clock = d as Clock;
if (!clock.IsLoaded)
{
return;
}
clock.CurrentTime = Convert.ToDateTime($"{clock.Hour}:{clock.Minute}");
if (e.NewValue != null)
{
var args = new RoutedPropertyChangedEventArgs(DateTime.Now, (DateTime)clock.CurrentTime);
args.RoutedEvent = CurrentTimeChangedEvent;
clock.RaiseEvent(args);
}
}
///
/// 确认时间
///
private void ConfirmButton_Click(object sender, RoutedEventArgs e)
{
var oldTime = SelectedTime;
var newTime = CurrentTime;
SelectedTime = CurrentTime;
var args = new RoutedPropertyChangedEventArgs(oldTime, newTime);
args.RoutedEvent = SelectedTimeChangedEvent;
RaiseEvent(args);
}
}
}