本文来自永恒的记忆的博客,原文地址:http://www.cnblogs.com/salam/archive/2010/07/12/1775451.html

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

  

  1.CalendarSample.xaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<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

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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是显示今天的日期

posted on 2011-09-14 09:17  hl3292  阅读(1193)  评论(4编辑  收藏  举报