【WPF】日历中拓展添加今天和清空按钮(菜鸟教程)
2017-03-29 16:44
357 查看
在xaml中<Grid>标签上面添加如下代码
2.在MainWindow.xaml.cs中添加如下代码(粘贴后会有错误,根据修补程序的提示添加相应using即可)
3.在xaml中<Grid>标签中添加如下代码(有时VS会提示不存在UserDatePicker,代码是没问题的,删掉下面的代码后启动一下再把下面的代码粘上就好了)
大功告成!!!
使用SelectedDateChanged属性时改成DatePicker.SelectedDateChanged="XXX"就可以了!
使用Name属性时改成x:Name就可以了!
运行截图:
<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就可以了!
运行截图:
相关文章推荐
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(二十) 第一部分拓展小结篇
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(二十) 第一部分拓展小结篇
- WPF 后台添加单选按钮组。仅供学习参考
- nc 自定义Action 添加拓展按钮的方法
- 添加图片按钮-UI界面编辑器(SkinStudio)教程
- 菜鸟也能当PS高手!PhotoWorks为你的图片添加漂亮的边框(教程+下载)
- iOS 9应用开发教程之使用代码添加按钮美化按钮
- 如何给wpf的按钮添加背景图片
- WPF按钮清空自带样式,以及透明按钮时,Grid的Background属性设置引起"点击"问题.
- C# WPF button样式清空以及关闭 最小化 按钮重置
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(二十) 第一部分拓展小结篇
- WPF自学教程系列2:如何在xaml文件添加引用?
- WPF 获取ListView中列添加LinkButton按钮,点击按钮的当前行的ID值
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(二十) 第一部分拓展小结篇
- WPF动态添加按钮并相应事件。
- 【Windows 8系统开始添加关机按钮教程】
- WPF 为按钮添加Tooltip
- 清空WPF自带样式,以及透明按钮
- NGUI使用教程(2) 使用NGUI创建2D场景并且添加标签和按钮
- iOS 9应用开发教程之使用代码添加按钮美化按钮