背水一战 Windows 10 (44) - 控件(日期类): CalendarView, DatePicker, TimePicker

简介: 原文:背水一战 Windows 10 (44) - 控件(日期类): CalendarView, DatePicker, TimePicker[源码下载] 背水一战 Windows 10 (44) - 控件(日期类): CalendarView, DatePicker, TimePicker ...
原文: 背水一战 Windows 10 (44) - 控件(日期类): CalendarView, DatePicker, TimePicker

[源码下载]


背水一战 Windows 10 (44) - 控件(日期类): CalendarView, DatePicker, TimePicker



作者:webabcd


介绍
背水一战 Windows 10 之 控件(日期类)

  • CalendarView
  • DatePicker
  • TimePicker



示例
1、CalendarView 的示例
Controls/DateControl/CalendarViewDemo.xaml

<Page
    x:Class="Windows10.Controls.DateControl.CalendarViewDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.DateControl"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    
    xmlns:common="using:Windows10.Common">

    <Page.Resources>
        <common:NullableBooleanToBooleanConverter x:Key="NullableBooleanToBooleanConverter" />
    </Page.Resources>
    
    <Grid Background="Transparent">
        <StackPanel Margin="10 0 10 10">

            <CheckBox Name="chkIsOutOfScopeEnabled" Margin="5" Content="IsOutOfScopeEnabled" IsChecked="True" />
            <CheckBox Name="chkIsGroupLabelVisible" Margin="5" Content="IsGroupLabelVisible" IsChecked="True" />
            <CheckBox Name="chkIsTodayHighlighted" Margin="5" Content="IsTodayHighlighted" IsChecked="True" />
            <ComboBox x:Name="cmbSelectionMode" Margin="5" PlaceholderText="SelectionMode" SelectionChanged="cmbSelectionMode_SelectionChanged">
                <ComboBoxItem>None</ComboBoxItem>
                <ComboBoxItem>Single</ComboBoxItem>
                <ComboBoxItem>Multiple</ComboBoxItem>
            </ComboBox>
            <ComboBox x:Name="cmbDisplayMode" Margin="5" PlaceholderText="DisplayMode" SelectionChanged="cmbDisplayMode_SelectionChanged">
                <ComboBoxItem>Month</ComboBoxItem>
                <ComboBoxItem>Year</ComboBoxItem>
                <ComboBoxItem>Decade</ComboBoxItem>
            </ComboBox>
            <ComboBox x:Name="cmbFirstDayOfWeek" Margin="5" PlaceholderText="FirstDayOfWeek" SelectionChanged="cmbFirstDayOfWeek_SelectionChanged">
                <ComboBoxItem>Sunday</ComboBoxItem>
                <ComboBoxItem>Monday</ComboBoxItem>
                <ComboBoxItem>Tuesday</ComboBoxItem>
                <ComboBoxItem>Wednesday</ComboBoxItem>
                <ComboBoxItem>Thursday</ComboBoxItem>
                <ComboBoxItem>Friday</ComboBoxItem>
                <ComboBoxItem>Saturday</ComboBoxItem>
            </ComboBox>

            <!--
                CalendarView - 日历控件
                    IsOutOfScopeEnabled - 是否以不同的颜色显示范围外的日历项
                    IsGroupLabelVisible - 是否在月的第一天或年的第一月的日历项中显示月份或年份
                    IsTodayHighlighted - 是否高亮显示当天日历项
                    FirstDayOfWeek - 指定每周的第一天是周几
                    SelectionMode - 日历的选择模式(None - 禁止选择, Single - 单选, Multiple - 多选)
                    DisplayMode - 日历的显示模式(Month - 月, Year - 年, Decade - 十年)
                    Language - 日历上的显示语言
                    CalendarIdentifier - 控件使用的日历系统,一个字符串值,在 CalendarIdentifiers 类中有封装
                    SelectedDatesChanged - 选中的日期发生变化时触发的事件
                    CalendarViewDayItemChanging - 加载日历项时触发的事件
            
                    其他大部分属性都是用于配置显示样式的了,具体说明详见文档
                    另外,在 CalendarView 控件中没有属性可以直接设置当天日历项的背景色(在本例的 code-behind 中给出了解决办法)
            -->
            <CalendarView Name="calendarView" Margin="5" 
                          IsOutOfScopeEnabled="{x:Bind chkIsOutOfScopeEnabled.IsChecked, Mode=TwoWay, Converter={StaticResource NullableBooleanToBooleanConverter}}" 
                          IsGroupLabelVisible="{x:Bind chkIsGroupLabelVisible.IsChecked, Mode=TwoWay, Converter={StaticResource NullableBooleanToBooleanConverter}}" 
                          IsTodayHighlighted="{x:Bind chkIsTodayHighlighted.IsChecked, Mode=TwoWay, Converter={StaticResource NullableBooleanToBooleanConverter}}" 
                          FirstDayOfWeek="Sunday"
                          SelectionMode="Single"
                          DisplayMode="Month"
                          Language="zh-cn" 
                          CalendarIdentifier="GregorianCalendar"
                          SelectedDatesChanged="calendarView_SelectedDatesChanged"
                          CalendarViewDayItemChanging="calendarView_CalendarViewDayItemChanging" />

        </StackPanel>
    </Grid>
</Page>

Controls/DateControl/CalendarViewDemo.xaml.cs

/*
 * CalendarView - 日历控件(继承自 Control, 请参见 /Controls/BaseControl/ControlDemo/)
 * 
 * CalendarViewDayItem - 日历项控件(继承自 Control, 请参见 /Controls/BaseControl/ControlDemo/)
 *     Date - 获取此日历项的日期(只读)
 *     IsBlackout - 此日历项是否不可用(通过 CalendarView 的 BlackoutForeground 属性可配置不可用日历项的前景色)
 */

using System;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace Windows10.Controls.DateControl
{
    public sealed partial class CalendarViewDemo : Page
    {
        public CalendarViewDemo()
        {
            this.InitializeComponent();
        }

        private void cmbSelectionMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            calendarView.SelectionMode = (CalendarViewSelectionMode)Enum.Parse(typeof(CalendarViewSelectionMode), (e.AddedItems[0] as ComboBoxItem).Content.ToString());
        }

        private void cmbDisplayMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            calendarView.DisplayMode = (CalendarViewDisplayMode)Enum.Parse(typeof(CalendarViewDisplayMode), (e.AddedItems[0] as ComboBoxItem).Content.ToString());
        }

        private void cmbFirstDayOfWeek_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            calendarView.FirstDayOfWeek = (Windows.Globalization.DayOfWeek)Enum.Parse(typeof(Windows.Globalization.DayOfWeek), (e.AddedItems[0] as ComboBoxItem).Content.ToString());
        }


        // 加载日历项时触发的事件
        private void calendarView_CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args)
        {
            // 如果当前加载的日历项时当天的话
            if (args.Item.Date.Date.Equals(DateTime.Now.Date))
            {
                // 修改日历项的背景色(在 CalendarView 控件中没有属性可以直接设置当天日历项的背景色)
                // 另外,还有一些日历项的样式无法通过 CalendarView 直接设置,那么都可以考虑像这样做
                args.Item.Background = new SolidColorBrush(Colors.Orange);
            }
        }

        // 选中的日期发生变化时触发的事件
        private void calendarView_SelectedDatesChanged(CalendarView sender, CalendarViewSelectedDatesChangedEventArgs args)
        {
            // args.RemovedDates - 之前被选中的日期集合
            // args.AddedDates - 当前被选中的日期集合

            // calendarView.SelectedDates - 当前被选中的日期集合
        }
    }
}


2、DatePicker 的示例
Controls/DateControl/DatePickerDemo.xaml

<Page
    x:Class="Windows10.Controls.DateControl.DatePickerDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.DateControl"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        <StackPanel Margin="10 0 10 10">

            <TextBlock Name="lblMsg" />

            <!--
                DatePicker - 日期选择控件
                    Header - 可以设置一个纯文本,不能命中测试,空 Header 的话不会占用任何空间
                    HeaderTemplate - 可以将 Header 设置为任何 xaml,且支持命中测试
                    DateChanged - 选中的日期发生变化时触发的事件
                    Orientation - 经测试,无效
                    YearFormat, MonthFormat, DayFormat - 格式化年, 月, 日数据(支持 format templates 和 format patterns)
            
            
                注:关于 format templates 和 format patterns 请参见
                http://msdn.microsoft.com/en-us/library/windows/apps/windows.globalization.datetimeformatting.datetimeformatter.aspx
            -->
            
            <DatePicker x:Name="datePicker" Header="Date" DateChanged="datePicker_DateChanged" Margin="5" Orientation="Vertical" />


            <!-- 
                通过格式模板(format templates)设置 DatePicker 的显示格式
            -->
            <DatePicker DayFormat="day" MonthFormat="month.numeric" YearFormat="year.abbreviated" Margin="5" />


            <!-- 
                通过格式模式(format patterns)设置 DatePicker 的显示格式
            -->
            <DatePicker DayFormat="{}{day.integer}({dayofweek.abbreviated})" MonthFormat="{}{month.integer(2)}" YearFormat="{}{year.full}" Margin="5" />
            <DatePicker DayFormat="{}{day.integer}日 ({dayofweek.abbreviated})" MonthFormat="{}{month.integer(2)}月" YearFormat="{}{year.full}年" Margin="5" />

        </StackPanel>
    </Grid>
</Page>

Controls/DateControl/DatePickerDemo.xaml.cs

/*
 * DatePicker - 日期选择控件(继承自 Control, 请参见 /Controls/BaseControl/ControlDemo/)
 *     YearVisible, MonthVisible, DayVisible, MinYear, MaxYear, Date, CalendarIdentifier - 详见下面示例代码中的注释
 */

using System;
using Windows.Globalization;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Windows10.Controls.DateControl
{
    public sealed partial class DatePickerDemo : Page
    {
        public DatePickerDemo()
        {
            this.InitializeComponent();

            this.Loaded += DatePickerDemo_Loaded;
        }

        void DatePickerDemo_Loaded(object sender, RoutedEventArgs e)
        {
            // Date - DatePicker 控件当前显示的日期
            datePicker.Date = DateTimeOffset.Now.AddMonths(1);

            // MinYear - DatePicker 控件所允许选择的最小的年份
            datePicker.MinYear = DateTimeOffset.Now.AddYears(-20);
            // MaxYear - DatePicker 控件所允许选择的最大的年份
            datePicker.MaxYear = DateTimeOffset.Now.AddYears(20);

            // YearVisible -  是否显示 year 选择框
            datePicker.YearVisible = true;
            // MonthVisible -  是否显示 month 选择框
            datePicker.MonthVisible = true;
            // DayVisible -  是否显示 day 选择框
            datePicker.DayVisible = true;

            // CalendarIdentifier - DatePicker 控件使用的日历系统,一个字符串值,在 CalendarIdentifiers 类中有封装
            datePicker.CalendarIdentifier = CalendarIdentifiers.Gregorian;
        }

        // DatePicker 控件选中的日期发生变化时触发的事件
        private void datePicker_DateChanged(object sender, DatePickerValueChangedEventArgs e)
        {
            // e.OldDate - 原日期
            // e.NewDate - 新日期
            lblMsg.Text = $"OldDate - {e.OldDate.ToString("yyyy-MM-dd hh:mm:ss")}, NewDate - {e.NewDate.ToString("yyyy-MM-dd hh:mm:ss")}";
        }
    }
}


3、TimePicker 的示例
Controls/DateControl/TimePickerDemo.xaml

<Page
    x:Class="Windows10.Controls.DateControl.TimePickerDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.DateControl"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        <StackPanel Margin="10 0 10 10">

            <TextBlock Name="lblMsg" Margin="5" />

            <!--
                TimePicker - 时间选择控件
                    Header - 可以设置一个纯文本,不能命中测试,空 Header 的话不会占用任何空间
                    HeaderTemplate - 可以将 Header 设置为任何 xaml,且支持命中测试
                    TimeChanged - 选中的时间发生变化时触发的事件
            -->
            <TimePicker Name="timePicker1" Header="Time" TimeChanged="timePicker1_TimeChanged" Margin="5" />

            <TimePicker Name="timePicker2" Header="Time" Margin="5" />

        </StackPanel>
    </Grid>
</Page>

Controls/DateControl/TimePickerDemo.xaml.cs

/*
 * TimePicker - 时间选择控件(继承自 Control, 请参见 /Controls/BaseControl/ControlDemo/)
 *     Time, MinuteIncrement, ClockIdentifier - 详见下面示例代码中的注释
 */

using System;
using Windows.Globalization;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Windows10.Controls.DateControl
{
    public sealed partial class TimePickerDemo : Page
    {
        public TimePickerDemo()
        {
            this.InitializeComponent();

            this.Loaded += TimePickerDemo_Loaded;
        }

        void TimePickerDemo_Loaded(object sender, RoutedEventArgs e)
        {
            // Time - TimePicker 控件当前显示的时间
            timePicker1.Time = new TimeSpan(16, 0, 0);

            // MinuteIncrement - 分钟选择框的分钟增量(0, 15, 30, 45)
            timePicker1.MinuteIncrement = 15;

            // ClockIdentifier - 小时制式,ClockIdentifiers.TwelveHour(12HourClock),12 小时制
            timePicker1.ClockIdentifier = ClockIdentifiers.TwelveHour;

            // ClockIdentifier - 小时制式,ClockIdentifiers.TwentyFourHour(24HourClock),24 小时制
            timePicker2.ClockIdentifier = ClockIdentifiers.TwentyFourHour;
        }

        // TimePicker 控件选中的时间发生变化时触发的事件
        private void timePicker1_TimeChanged(object sender, TimePickerValueChangedEventArgs e)
        {
            // e.OldTime - 原时间
            // e.NewTime - 新时间
            lblMsg.Text = $"OldTime - {e.OldTime.ToString("c")}, NewTime - {e.NewTime.ToString("c")}";
        }
    }
}



OK
[源码下载]

目录
相关文章
|
4月前
|
JavaScript Linux C#
【傻瓜级JS-DLL-WINCC-PLC交互】1.C#用windows窗体控件创建.net控件
【傻瓜级JS-DLL-WINCC-PLC交互】1.C#用windows窗体控件创建.net控件
65 0
|
11月前
|
C++ Windows
C++ Windows窗口程序:子窗口控件之按钮类button
C++ Windows窗口程序:子窗口控件之按钮类button
555 0
|
API C# Windows
C#实现操作Windows窗口句柄:遍历、查找窗体和控件【窗口句柄最全总结之一】
C#对Windows窗口或窗口句柄的操作,都是通过 P/Invoke Win32 API 实现的,DllImport引入Windows API操作窗口(句柄),可以实现枚举已打开的窗口、向窗口...
2059 0
C#实现操作Windows窗口句柄:遍历、查找窗体和控件【窗口句柄最全总结之一】
|
Windows
Windows程序设计——Windows单选按钮、复选框、分组框控件
Windows程序设计——Windows单选按钮、复选框、分组框控件
524 0
Windows程序设计——Windows单选按钮、复选框、分组框控件
|
Windows
Windows程序设计——(源代码)Windows单选按钮、复选框、分组框控件
Windows程序设计——(源代码)Windows单选按钮、复选框、分组框控件
166 0
|
Windows
Windows 技术篇 - windows日期和时间设置里没有Internet 时间页签原因和解决方法
因为工作关系设置了一下系统时间,然后想用网络获取最新的时间来自动更正下,然后发现没有这个功能…,百度后发现其实是有一个 Internet 时间页签的,在这个页签里才可以设置,那为什么我这里没有呢?
894 0
Windows 技术篇 - windows日期和时间设置里没有Internet 时间页签原因和解决方法
windows窗口中控件的样式
windows窗口中控件的样式一.按钮样式 button BS_AUTO3STATE 创建一个与三态复选框相同的按钮,但该框在用户选择时更改其状态。状态循环通过检查,不确定和清除。 BS_AUTOCHECKBOX 创建一个与复选框相同的按钮,但每次用户选中复选框时,检查状态会自动在已选中和已清除之间切换。
1286 0
|
C# Windows 开发工具
WindowsXamlHost:在 WPF 中使用 UWP 的控件(Windows Community Toolkit)
原文 WindowsXamlHost:在 WPF 中使用 UWP 的控件(Windows Community Toolkit) Windows Community Toolkit 再次更新到 5.0。
1909 0
|
API Windows 开发工具
使用 Microsoft.UI.Xaml 解决 UWP 控件和对老版本 Windows 10 的兼容性问题
原文 使用 Microsoft.UI.Xaml 解决 UWP 控件和对老版本 Windows 10 的兼容性问题 虽然微软宣称 Windows 10 将是最后一个 Windows 版本,但由于年代跨越实在太久远,兼容性依然是避不开的问题。
1851 0
|
定位技术 Android开发 iOS开发
背水一战 Windows 10 (66) - 控件(WebView): 监听和处理 WebView 的事件
原文:背水一战 Windows 10 (66) - 控件(WebView): 监听和处理 WebView 的事件 [源码下载] 背水一战 Windows 10 (66) - 控件(WebView): 监听和处理 WebView 的事件 作者:webabcd介绍背水一战 Windows 10 之 ...
1297 0