UI⑥Control族控件DatePicker

Object->DispatcherObject->DependencyObject->Visual->UIElement->FrameworkElement->Control->DatePicker


DatePicker

表示一个允许用户选择日期的控件。

DatePicker控件允许用户通过在Calendar 文本字段中键入日期或使用下拉控件来选择日期 。该DatePicker的仅支持公历。

DatePicker控件的许多属性是用于管理其内置的 Calendar ,并且与 Calendar 中的等效属性的功能相同。 特别是,DatePicker.IsTodayHighlighted、  DatePicker.FirstDayOfWeek、 DatePicker.BlackoutDates、 DatePicker.DisplayDateStart 、DatePicker.DisplayDateEnd 、 DatePicker.DisplayDate 和 DatePicker.SelectedDate 属性的功能与其Calendar 对应项的作用相同。 有关详细信息,请参阅 Calendar

用户可以直接在文本字段中键入日期,以设置 Text 属性。 如果 DatePicker 无法将输入的字符串转换为有效的日期,则会引发 DateValidationError 事件。 默认情况下,这会引发异常,但的DateValidationError 事件处理程序可以将 ThrowException 属性设置为 false ,并防止引发异常。

自定义 DatePicker 控件

若要对多个 DatePicker 控件应用相同的属性设置,请使用 Style 属性。 您可以修改 ControlTemplate 默认值,为控件指定独特的外观。 有关创建 ControlTemplate 的详细信息,请参阅 通过创建 System.windows.controls.controltemplate> 自定义现有控件的外观。 若要查看特定于 DatePicker 的部分和状态,请参阅 DatePicker 样式和模板

Note

设置视觉属性仅在该属性同时存在于控件的默认模板中并且通过使用TemplateBinding进行设置时才有效。您可以在通过创建ControlTemplate定制现有控件的外观的更改控件的视觉结构”部分中找到视觉属性列表。

名称备注权限
CalendarStyleProperty标识 CalendarStyle 依赖项属性。public static readonly
DisplayDateEndProperty标识 DisplayDateEnd 依赖项属性。public static readonly
DisplayDateProperty标识 DisplayDate 依赖项属性。public static readonly
DisplayDateStartProperty标识 DisplayDateStart 依赖项属性。public static readonly
FirstDayOfWeekProperty标识 FirstDayOfWeek 依赖项属性。public static readonly
IsDropDownOpenProperty标识 IsDropDownOpen 依赖项属性。public static readonly
IsTodayHighlightedProperty标识 IsTodayHighlighted 依赖项属性。public static readonly
SelectedDateChangedEvent标识 SelectedDateChanged 路由事件。public static readonly
SelectedDateFormatProperty标识 SelectedDateFormat 依赖项属性。public static readonly
SelectedDateProperty标识 SelectedDate 依赖项属性。public static readonly
TextProperty标识 Text 依赖项属性。public static readonly
名称备注权限
BlackoutDates获取或设置标记为不可选择的日期的集合。get;
CalendarStyle获取或设置在呈现日历时使用的样式。get; set;
DisplayDate获取或设置要显示的日期。get; set;
DisplayDateEnd获取或设置要显示的最后一个日期。get; set;
DisplayDateStart获取或设置要显示的第一个日期。get; set;
FirstDayOfWeek获取或设置被视为一周开始的日期。get; set;
HasEffectiveKeyboardFocus获取一个值,该值指示 DatePicker 是否具有焦点。get;
IsDropDownOpen获取或设置一个值,该值指示是打开还是关闭下拉 Calendarget; set;
IsTodayHighlighted获取或设置一个值,该值指示是否突出显示当前日期。get; set;
SelectedDate获取或设置当前选定的日期。get; set;
SelectedDateFormat获取或设置用于显示选定日期的格式。get; set;
Text获取由 DatePicker 显示的文本,或设置选定日期。get; set;
名称备注权限

OnApplyTemplate

当应用新模板时生成 DatePicker 控件的可视化树。protected

OnCalendarClosed

引发 CalendarClosed 路由事件。protected

OnCalendarOpened

引发 CalendarOpened 路由事件。protected

OnCreateAutomationPeer

返回一个 DatePickerAutomationPeer,供自动化基础结构使用。protected

OnDateValidationError

引发 DateValidationError 事件。protected

OnSelectedDateChanged

引发 SelectedDateChanged 路由事件。protected

ToString

提供选定日期的文本表示形式。public
名称备注

CalendarClosed

当关闭下拉 Calendar 时发生。

CalendarOpened

当打开下拉 Calendar 时发生。

DateValidationError

当 Text 设置为无法解释为日期的值或者无法选择日期时发生。

SelectedDateChanged

当 SelectedDate 属性更改时发生。


XAML范例

<Window
    x:Class="DatePickerDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:DatePickerDemo"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="400"
    Height="450"
    mc:Ignorable="d">
    <StackPanel>      
        <TextBlock/>
        <TextBox/>
        <Button Content="设置日期5/16/2021" Click="OnClick"/>
        <DatePicker
            DisplayDate="5/2/2021"
            DisplayDateEnd="12/1/2021"
            DisplayDateStart="1/1/2021"
            FirstDayOfWeek="Friday"
            IsDropDownOpen="True"
            IsTodayHighlighted="True"
            SelectedDate="5/5/2021"
            SelectedDateFormat="Short"
            CalendarOpened="OnCalendarOpened"
            CalendarClosed="OnCalendarClosed"
            SelectedDateChanged="OnSelectedDateChanged"
            DateValidationError="OnDateValidationError" >
            <DatePicker.BlackoutDates>
                <CalendarDateRange End="5/23/2021" Start="5/20/2021" />
                <CalendarDateRange End="5/29/2021" Start="5/26/2021" />
            </DatePicker.BlackoutDates>
        </DatePicker>
    </StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DatePickerDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OnDateValidationError(object sender, DatePickerDateValidationErrorEventArgs e)
        {
            MessageBox.Show("OnDateValidationError");
            //引发异常
            e.ThrowException = true;
        }

        private void OnCalendarOpened(object sender, RoutedEventArgs e)
        {
            foreach (var item in ((sender as DatePicker).Parent as StackPanel).Children)
            {
                if (item is TextBlock) (item as TextBlock).Text = "OnCalendarOpened";
            }           
        }

        private void OnCalendarClosed(object sender, RoutedEventArgs e)
        {
            foreach (var item in ((sender as DatePicker).Parent as StackPanel).Children)
            {
                if (item is TextBlock) (item as TextBlock).Text = "OnCalendarClosed";
            }
        }

        private void OnSelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (var item in ((sender as DatePicker).Parent as StackPanel).Children)
            {
                if (item is TextBox) (item as TextBox).Text = (sender as DatePicker).Text;
            }
        }

        private void OnClick(object sender, RoutedEventArgs e)
        {
            foreach (var item in ((sender as Button).Parent as StackPanel).Children)
            {
                if (item is DatePicker) 
                    (item as DatePicker).SelectedDate = new DateTime(2021,5,16);
            }
        }
    }
}

C#范例

<Window
    x:Class="DatePickerDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:DatePickerDemo"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="400"
    Height="450"
    mc:Ignorable="d">
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DatePickerDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            TextBlock textBlock = new TextBlock();
            TextBox textBox = new TextBox();
            Button button = new Button { Content = "设置日期5/16/2021" };
            button.Click += OnClick;

            DatePicker datePicker = new DatePicker { DisplayDate = new DateTime(2021, 5, 2),
                DisplayDateStart = new DateTime(2021, 1, 1),
                DisplayDateEnd = new DateTime(2021, 12, 1),
                FirstDayOfWeek = DayOfWeek.Friday,
            IsDropDownOpen=true,
                IsTodayHighlighted=true,
                SelectedDate = new DateTime(2021, 5, 5),
                SelectedDateFormat=DatePickerFormat.Short,
                BlackoutDates = { new CalendarDateRange { Start=new DateTime(2021, 5, 20),End = new DateTime(2021, 5, 23) }, 
                    new CalendarDateRange { Start = new DateTime(2021, 5, 26), End = new DateTime(2021, 5, 29) } },
            };

            datePicker.CalendarOpened += OnCalendarOpened;
            datePicker.CalendarClosed += OnCalendarClosed;
            datePicker.SelectedDateChanged += OnSelectedDateChanged;
            datePicker.DateValidationError += OnDateValidationError;

            StackPanel stackPanel = new StackPanel();
            stackPanel.Children.Add(textBlock);
            stackPanel.Children.Add(textBox);
            stackPanel.Children.Add(button);
            stackPanel.Children.Add(datePicker);

            this.Content = stackPanel;
        }

        private void OnDateValidationError(object sender, DatePickerDateValidationErrorEventArgs e)
        {
            MessageBox.Show("OnDateValidationError");
            //引发异常
            e.ThrowException = true;
        }

        private void OnCalendarOpened(object sender, RoutedEventArgs e)
        {
            foreach (var item in ((sender as DatePicker).Parent as StackPanel).Children)
            {
                if (item is TextBlock) (item as TextBlock).Text = "OnCalendarOpened";
            }           
        }

        private void OnCalendarClosed(object sender, RoutedEventArgs e)
        {
            foreach (var item in ((sender as DatePicker).Parent as StackPanel).Children)
            {
                if (item is TextBlock) (item as TextBlock).Text = "OnCalendarClosed";
            }
        }

        private void OnSelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (var item in ((sender as DatePicker).Parent as StackPanel).Children)
            {
                if (item is TextBox) (item as TextBox).Text = (sender as DatePicker).Text;
            }
        }

        private void OnClick(object sender, RoutedEventArgs e)
        {
            foreach (var item in ((sender as Button).Parent as StackPanel).Children)
            {
                if (item is DatePicker) 
                    (item as DatePicker).SelectedDate = new DateTime(2021,5,16);
            }
        }
    }
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值