You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.9 KiB
C#

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
namespace XGLFinishPro.Tools
{
public class UserDatePicker : DatePicker
{
//日期文本框
private DatePickerTextBox textBox;
private Popup popup;
//今天
private TextBlock goToday;
//清空
private TextBlock goClear;
#region 重载初始化
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
textBox = this.GetTemplateChild("PART_TextBox") as DatePickerTextBox;
popup = this.GetTemplateChild("PART_Popup") as Popup;
popup.Width = 300;
popup.Height = 300;
if (AlternativeCalendarStyle != null)
{
System.Windows.Controls.Calendar calendar = popup.Child as System.Windows.Controls.Calendar;
calendar.Style = AlternativeCalendarStyle;
calendar.ApplyTemplate();
goToday = calendar.Template.FindName("PART_GoToday", calendar) as TextBlock;
if (goToday != null)
{
goToday.MouseLeftButtonUp -= new MouseButtonEventHandler(goToday_MouseLeftButtonUp);
goToday.MouseLeftButtonUp += new MouseButtonEventHandler(goToday_MouseLeftButtonUp);
}
goClear = calendar.Template.FindName("PART_GoClear", calendar) as TextBlock;
if (goClear != null)
{
goClear.MouseLeftButtonUp -= new MouseButtonEventHandler(goClear_MouseLeftButtonUp);
goClear.MouseLeftButtonUp += new MouseButtonEventHandler(goClear_MouseLeftButtonUp);
}
}
this.SelectedDateFormat = DatePickerFormat.Short;
}
#endregion
/// <summary>
/// AlternativeCalendarStyle Dependency Property
/// </summary>
public static readonly DependencyProperty AlternativeCalendarStyleProperty =
DependencyProperty.Register("AlternativeCalendarStyle", typeof(Style), typeof(UserDatePicker),
new FrameworkPropertyMetadata((Style)null,
null));
public Style AlternativeCalendarStyle
{
get { return (Style)GetValue(AlternativeCalendarStyleProperty); }
set { SetValue(AlternativeCalendarStyleProperty, value); }
}
#region 今天
private void goToday_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
this.SelectedDate = DateTime.Now;
popup.IsOpen = false;
}
#endregion
#region 清空
private void goClear_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
this.SelectedDate = null;
popup.IsOpen = false;
}
#endregion
}
}