Silverlight学习笔记五DatePicker和Calendar控件

DatePicker、Calendar是SilverLight提供的日期控件。下面是一个例子

1.CalendarSample.xaml

<UserControl xmlns:sdk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"  x:Class="Silverlight.Common.View.CalendarSample"
    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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" Loaded="UserControl_Loaded">
   
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="95" />
            <ColumnDefinition Width="160" />
            <ColumnDefinition Width="60" />
            <ColumnDefinition Width="100" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <TextBlock Text="您选择的日期是:" Height="27" />
        <TextBox  Grid.Row="0" Grid.Column="1" x:Name="txtSelectedDate" IsReadOnly="true" />

        <TextBlock Grid.Row="1" Grid.Column="0" Text="今天是:" />
        <TextBox  Grid.Row="1" Grid.Column="1" x:Name="txtDisplayDate" IsReadOnly="true" />

        <TextBlock Grid.Row="2" Grid.Column="0" Text="开始日期:" />
        <sdk:DatePicker
              x:Name="dateStart"
              Grid.Row="2"
              Grid.Column="1"
           SelectedDateFormat="Short"
              SelectedDateChanged="dateStart_SelectedDateChanged"
              Margin="5" TabNavigation="Local" />

        <TextBlock Grid.Row="3" Grid.Column="0" Text="截止日期:" />
        <sdk:DatePicker
              x:Name="dateEnd"
              Grid.Row="3"
              Grid.Column="1"
              SelectedDateChanged="dateEnd_SelectedDateChanged"
              Margin="5" />

       
        <sdk:Calendar
              x:Name="Calendar1"
              Grid.Row="6"
              Grid.Column="0"
              Grid.ColumnSpan="2"
          
              SelectedDatesChanged="Calendar1_SelectedDatesChanged"
              Margin="5" />
        <TextBlock Text="日历样式:" Grid.Column="2"></TextBlock>
        <ComboBox x:Name="cmbDisplayMode" SelectionChanged="cmbDisplayMode_SelectionChanged"  Grid.Column="3">
            <ComboBoxItem Content="Year"></ComboBoxItem>
            <ComboBoxItem Content="Decade"></ComboBoxItem>
            <ComboBoxItem Content="Month"></ComboBoxItem>
        </ComboBox>
        <TextBlock Text="显示格式:" Grid.Column="2" Grid.Row="1"></TextBlock>
        <ComboBox x:Name="cmbDisplayFormat" SelectionChanged="cmbDisplayFormat_SelectionChanged" Grid.Row="1" Grid.Column="3">
            <ComboBoxItem Content="短日期型"></ComboBoxItem>
            <ComboBoxItem Content="长日期型(包含星期)"></ComboBoxItem>
            <ComboBoxItem Content="时间型"></ComboBoxItem>
            <ComboBoxItem Content="长时间型"></ComboBoxItem>
        </ComboBox>
   
    </Grid>
</UserControl>

 

2.CalendarSample.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Silverlight.Common.View
{
    public partial class CalendarSample : UserControl
    {
        public CalendarSample()
        {
            InitializeComponent();
        }

        private void dateStart_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.Calendar1 == null)
            {
                return;
            }

            try
            {
                this.Calendar1.DisplayDateStart = e.AddedItems[0] as DateTime?;
            }
            catch
            {
                this.Calendar1.DisplayDateStart = null;
                dateStart.Text = "";
            }
        }

        private void dateEnd_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.Calendar1 == null)
            {
                return;
            }

            try
            {
                this.Calendar1.DisplayDateEnd = e.AddedItems[0] as DateTime?;
            }
            catch
            {
                this.Calendar1.DisplayDateEnd = null;
                dateStart.Text = "";
            }
        }

        private void Calendar1_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Calendar1.SelectedDate != null)
            {
                DateTime date = (DateTime)this.Calendar1.SelectedDate;
                this.txtSelectedDate.Text = date.ToShortDateString();
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (Calendar1 != null)
            {
                this.txtDisplayDate.Text = this.Calendar1.DisplayDate.ToShortDateString();
            }
        }

        private void cmbDisplayFormat_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (this.cmbDisplayFormat.SelectedIndex)
            {
                case 0:
                    this.txtDisplayDate.Text = DateTime.Now.ToShortDateString();
                    break;

                case 1:
                    this.txtDisplayDate.Text = DateTime.Now.ToLongDateString();
                    break;

                case 2:
                    this.txtDisplayDate.Text = DateTime.Now.ToShortTimeString();
                    break;

                case 3:
                    this.txtDisplayDate.Text = DateTime.Now.ToLongTimeString();
                    break;

                default:
                    this.txtDisplayDate.Text = DateTime.Now.ToShortDateString();
                    break;
            }
        }

        private void cmbDisplayMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (this.cmbDisplayMode.SelectedIndex)
            {
                case 0:
                    this.Calendar1.DisplayMode = CalendarMode.Year;
                    break;

                case 1:
                    this.Calendar1.DisplayMode = CalendarMode.Decade;
                    break;

                default:
                    this.Calendar1.DisplayMode = CalendarMode.Month;
                    break;
            }
        }
    }
}

 

注:DisplayMode是日期显示的样式,它有三个值『Year,Decade,Month』

  DisplayDate是显示今天的日期

源码下载:https://files.cnblogs.com/salam/Silverlight.Common.rar

 

posted @ 2010-07-12 01:54  ForrestWoo  阅读(7201)  评论(2编辑  收藏  举报