您的位置:首页 > 其它

【WPF】日历中拓展添加今天和清空按钮(菜鸟教程)

2017-03-29 16:44 357 查看
在xaml中<Grid>标签上面添加如下代码
<Window.Resources>
<Style x:Key="calendarWithGotToTodayStyle"
TargetType="{x:Type Calendar}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Calendar}">
<Border >
<StackPanel x:Name="PART_Root"
HorizontalAlignment="Center" Background="#FF878787">
<CalendarItem x:Name="PART_CalendarItem"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Style="{TemplateBinding CalendarItemStyle}" />
<Grid VerticalAlignment="Top" HorizontalAlignment="Center">

<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"></ColumnDefinition>
<ColumnDefinition Width="30"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock x:Name="PART_GoToday"
Width="30" Height="20"
Text="今天" VerticalAlignment="Bottom" HorizontalAlignment="Center" TextAlignment="Center" Cursor="Hand"/>
<TextBlock Grid.Column="2" x:Name="PART_GoClear"
Width="30" Height="20"
Text="清空"  VerticalAlignment="Bottom" HorizontalAlignment="Center" TextAlignment="Center" Cursor="Hand"/>
</Grid>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

</Window.Resources>


2.在MainWindow.xaml.cs中添加如下代码(粘贴后会有错误,根据修补程序的提示添加相应using即可)
public class UserDatePicker : DatePicker
{

//日期文本框
public 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;

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;
goClear = calendar.Template.FindName("PART_GoClear", calendar) as TextBlock;

goToday.MouseLeftButtonUp -= new MouseButtonEventHandler(goToday_MouseLeftButtonUp);
goToday.MouseLeftButtonUp += new MouseButtonEventHandler(goToday_MouseLeftButtonUp);

goClear.MouseLeftButtonUp -= new MouseButtonEventHandler(goClear_MouseLeftButtonUp);
goClear.MouseLeftButtonUp += new MouseButtonEventHandler(goClear_MouseLeftButtonUp);

this.SelectedDateFormat = DatePickerFormat.Short;//格式转换:xxxx年xx月xx日->xxxx/xx/xx

}
#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
}


3.在xaml中<Grid>标签中添加如下代码(有时VS会提示不存在UserDatePicker,代码是没问题的,删掉下面的代码后启动一下再把下面的代码粘上就好了)
<local:UserDatePicker AlternativeCalendarStyle="{StaticResource calendarWithGotToTodayStyle}" SelectedDateFormat="Long"/>


大功告成!!!

使用SelectedDateChanged属性时改成DatePicker.SelectedDateChanged="XXX"就可以了!
使用Name属性时改成x:Name就可以了!

运行截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: