重新想象 Windows 8.1 Store Apps (73) - 新增控件: DatePicker, TimePicker

原文:重新想象 Windows 8.1 Store Apps (73) - 新增控件: DatePicker, TimePicker

[源码下载]

作者:webabcd

介绍
重新想象 Windows 8.1 Store Apps 之新增控件

  • DatePicker - 日期选择控件
  • TimePicker - 时间选择控件

示例
1、演示 DatePicker 的应用
DatePickerDemo.xaml

<Page
    x:Class="Windows81.Controls.DatePickerDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows81.Controls"
    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="120 0 0 0">

            <TextBlock Name="lblMsg" FontSize="14.667" />

            <!--
                DatePicker - 日期选择控件(默认横排,需要竖排的话则设置 Orientation="Vertical" 即可)
                    Header - 控件的标题
                    DateChanged - 选中的日期发生变化时所触发的事件
            -->
            <DatePicker x:Name="datePicker" Header="Date" DateChanged="datePicker_DateChanged" Margin="0 20 0 0" />

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

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

            <!--
                关于 format templates 和 format patterns 请参见:
                http://msdn.microsoft.com/en-us/library/windows/apps/windows.globalization.datetimeformatting.datetimeformatter.aspx
            -->

        </StackPanel>
    </Grid>

</Page>

DatePickerDemo.xaml.cs

/*
 * DatePicker - 日期选择控件
 */

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

namespace Windows81.Controls
{
    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 控件所使用的日历系统(Gregorian, Hebrew, Hijri, Japanese, Julian, Korean, Taiwan, Thai, UmAlQura)
            datePicker.CalendarIdentifier = CalendarIdentifiers.Gregorian;
        }

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

2、演示 TimePicker 的应用
TimePickerDemo.xaml

<Page
    x:Class="Windows81.Controls.TimePickerDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows81.Controls"
    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="120 0 0 0">

            <TextBlock Name="lblMsg" FontSize="14.667" />

            <!--
                TimePicker - 时间选择控件(默认横排,需要竖排的话则设置 Orientation="Vertical" 即可)
                    Header - 控件的标题
                    TimeChanged - 选中的时间发生变化时所触发的事件
            -->
            <TimePicker Name="timePicker" Header="Time" TimeChanged="timePicker_TimeChanged" Margin="0 20 0 0" />

            <TimePicker Name="timePicker2" Header="Time" Margin="0 20 0 0" />

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

TimePickerDemo.xaml.cs

/*
 * TimePicker - 时间选择控件
 */

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

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

            this.Loaded += TimePickerDemo_Loaded;
        }

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

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

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

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

OK
[源码下载]

时间: 2024-07-30 18:09:49

重新想象 Windows 8.1 Store Apps (73) - 新增控件: DatePicker, TimePicker的相关文章

重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar

[源码下载] 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar 作者:webabcd介绍重新想象 Windows 8.1 Store Apps 之新增控件 AppBar - 应用程序栏控件(新增了 AppBarButton, AppBarToggleButton, AppBarSeparator) CommandBar - 应用程序栏控件(AppBar 简化版) 示例1.演示 AppBar 的应用AppBarDemo.xaml

重新想象 Windows 8.1 Store Apps (74) - 新增控件: Flyout, MenuFlyout, SettingsFlyout

原文:重新想象 Windows 8.1 Store Apps (74) - 新增控件: Flyout, MenuFlyout, SettingsFlyout [源码下载] 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之新增控件 Flyout - Flyout 控件 MenuFlyout - 菜单 Flyout 控件 SettingsFlyout - 设置面板 Flyout 控件 示例1.演示 Flyout 的应用FlyoutDemo.xaml <Page x

重新想象 Windows 8.1 Store Apps (75) - 新增控件: Hub, Hyperlink

原文:重新想象 Windows 8.1 Store Apps (75) - 新增控件: Hub, Hyperlink [源码下载] 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之新增控件 Hub - 中心控件(由一个 header 和多个 section 组成) Hyperlink - 超链接控件(在 RichEditBox, RichTextBlock, RichTextBlockOverflow 内使用) 示例1.演示 Hub 的应用HubDemo.xa

重新想象 Windows 8.1 Store Apps (76) - 新增控件: SearchBox

今天有幸被召回母校给即将毕业的学弟学妹们讲我这两年的工作史,看了下母校没啥特别的变化,就是寝室都安了空调,学妹们都非常漂亮而已..好了不扯蛋了,说下今天的主题吧.这些天我在深度定制语法高亮功能的同时发现了博客园提供的一些有意思的函数,甚至有几个博客园都没用到,我也不知道怎么才能触发那些功能..打开这个js就可以看到很多好用的东西了,虽然写的不怎么样,但是至少有这些功能. ps: 推荐安装一个代码格式化的插件,否则一坨看着蛋疼.比如第一个就是 log,方便调试. www.qidian.com/Bo

重新想象 Windows 8.1 Store Apps (74) - 新增控件

今天有幸被召回母校给即将毕业的学弟学妹们讲我这两年的工作史,看了下母校没啥特别的变化,就是寝室都安了空调,学妹们都非常漂亮而已..好了不扯蛋了,说下今天的主题吧.这些天我在深度定制语法高亮功能的同时发现了博客园提供的一些有意思的函数,甚至有几个博客园都没用到,我也不知道怎么才能触发那些功能..打开这个js就可以看到很多好用的东西了,虽然写的不怎么样,但是至少有这些功能. ps: 推荐安装一个代码格式化的插件,否则一坨看着蛋疼.比如第一个就是 log,方便调试. www.qidian.com/Bo

重新想象 Windows 8.1 Store Apps 系列文章索引

[源码下载] [重新想象 Windows 8 Store Apps 系列文章] 作者:webabcd 1.重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar 介绍重新想象 Windows 8.1 Store Apps 之新增控件 AppBar - 应用程序栏控件(新增了 AppBarButton, AppBarToggleButton, AppBarSeparator) CommandBar - 应用程序栏控件(AppBar 简

重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件增加了 PlaceholderText 属性

[源码下载] 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件增加了 PlaceholderText 属性 作者:webabcd介绍重新想象 Windows 8.1 Store Apps 之控件增强 文本类控件的增强 为一些控件增加了 Header 属性和 HeaderTemplate 属性 为一些控件增加了 PlaceholderText 属性 示例1.演示

重新想象 Windows 8.1 Store Apps (82) - 绑定: DataContextChanged, TargetNullValue, FallbackValue, UpdateSourceTrigger

[源码下载] 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之绑定 DataContextChanged - FrameworkElement 的 DataContext 发生变化时触发的事件 TargetNullValue - 当绑定数据为 null 时所需要显示的值 FallbackValue - 当绑定失败(无法返回值)的时候所需要显示的值 UpdateSourceTrigger - UI 上数据更新的触发方式 示例1.演示 DataContextCha

重新想象 Windows 8.1 Store Apps (92) - 其他新特性: CoreDispatcher, 日历, 自定义锁屏系列图片

[源码下载] 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之其他新特性 CoreDispatcher 的新特性 “日历”的相关操作 自定义锁屏时需要显示的系列图片 示例1.演示 CoreDispatcher 在 win8.1 中的新特性CoreDispatcherDemo.xaml.cs /* * 演示 CoreDispatcher 在 win8.1 中的新特性 * * 关于几个 Core 的基础请参见:http://www.cnblogs.com/weba