Calendar.SelectedDates 属性

定义

获取选定日期的集合。

public:
 property System::Windows::Controls::SelectedDatesCollection ^ SelectedDates { System::Windows::Controls::SelectedDatesCollection ^ get(); };
public System.Windows.Controls.SelectedDatesCollection SelectedDates { get; }
member this.SelectedDates : System.Windows.Controls.SelectedDatesCollection
Public ReadOnly Property SelectedDates As SelectedDatesCollection

属性值

包含当前选定日期的 SelectedDatesCollection 对象。 默认值为空集合。

示例

以下示例创建一个 Calendar ,它选择了多个日期范围。

  // Create a Calendar that displays dates through
  // Januarary 31, 2009 and has dates that are not selectable.
  Calendar calendarWithBlackoutDates = new Calendar();
  calendarWithBlackoutDates.IsTodayHighlighted = false;
  calendarWithBlackoutDates.DisplayDate = new DateTime(2009, 1, 1);
  calendarWithBlackoutDates.DisplayDateEnd = new DateTime(2009, 1, 31);
  calendarWithBlackoutDates.SelectionMode = CalendarSelectionMode.MultipleRange;

  // Add the dates that are not selectable.
  calendarWithBlackoutDates.BlackoutDates.Add(
      new CalendarDateRange(new DateTime(2009, 1, 2), new DateTime(2009, 1, 4)));
  calendarWithBlackoutDates.BlackoutDates.Add(
      new CalendarDateRange(new DateTime(2009, 1, 9)));
  calendarWithBlackoutDates.BlackoutDates.Add(
      new CalendarDateRange(new DateTime(2009, 1, 16)));
  calendarWithBlackoutDates.BlackoutDates.Add(
      new CalendarDateRange(new DateTime(2009, 1, 23), new DateTime(2009, 1, 25)));
  calendarWithBlackoutDates.BlackoutDates.Add(
      new CalendarDateRange(new DateTime(2009, 1, 30)));

  // Add the selected dates.
  calendarWithBlackoutDates.SelectedDates.Add(
      new DateTime(2009, 1, 5));
  calendarWithBlackoutDates.SelectedDates.AddRange(
      new DateTime(2009, 1, 12), new DateTime(2009, 1, 15));
  calendarWithBlackoutDates.SelectedDates.Add(
      new DateTime(2009, 1, 27));

  // root is a Panel that is defined elswhere.
  root.Children.Add(calendarWithBlackoutDates);
' Create a Calendar that displays dates through 
' Januarary 31, 2009 and has dates that are not selectable. 
Dim calendarWithBlackoutDates As New Calendar()
calendarWithBlackoutDates.IsTodayHighlighted = False
calendarWithBlackoutDates.DisplayDate = New DateTime(2009, 1, 1)
calendarWithBlackoutDates.DisplayDateEnd = New DateTime(2009, 1, 31)
calendarWithBlackoutDates.SelectionMode = CalendarSelectionMode.MultipleRange

' Add the dates that are not selectable. 
calendarWithBlackoutDates.BlackoutDates.Add(New CalendarDateRange(New DateTime(2009, 1, 2), New DateTime(2009, 1, 4)))
calendarWithBlackoutDates.BlackoutDates.Add(New CalendarDateRange(New DateTime(2009, 1, 9)))
calendarWithBlackoutDates.BlackoutDates.Add(New CalendarDateRange(New DateTime(2009, 1, 16)))
calendarWithBlackoutDates.BlackoutDates.Add(New CalendarDateRange(New DateTime(2009, 1, 23), New DateTime(2009, 1, 25)))
calendarWithBlackoutDates.BlackoutDates.Add(New CalendarDateRange(New DateTime(2009, 1, 30)))

' Add the selected dates. 
calendarWithBlackoutDates.SelectedDates.Add(New DateTime(2009, 1, 5))
calendarWithBlackoutDates.SelectedDates.AddRange(New DateTime(2009, 1, 12), New DateTime(2009, 1, 15))
calendarWithBlackoutDates.SelectedDates.Add(New DateTime(2009, 1, 27))

' root is a Panel that is defined elswhere. 
root.Children.Add(calendarWithBlackoutDates)
<!-- Create a Calendar that displays dates through
     Januarary 31, 2009 and has dates that are not selectable. -->
<Calendar Margin="20" SelectionMode="MultipleRange"  
          IsTodayHighlighted="false" 
          DisplayDate="1/1/2009"
          DisplayDateEnd="1/31/2009"
          xmlns:sys="clr-namespace:System;assembly=mscorlib">

  <Calendar.BlackoutDates>
    <CalendarDateRange Start="1/2/2009" End="1/4/2009"/>
    <CalendarDateRange Start="1/9/2009" End="1/9/2009"/>
    <CalendarDateRange Start="1/16/2009" End="1/16/2009"/>
    <CalendarDateRange Start="1/23/2009" End="1/25/2009"/>
    <CalendarDateRange Start="1/30/2009" End="1/30/2009"/>
  </Calendar.BlackoutDates>

  <Calendar.SelectedDates>
    <sys:DateTime>1/5/2009</sys:DateTime>
    <sys:DateTime>1/12/2009</sys:DateTime>
    <sys:DateTime>1/14/2009</sys:DateTime>
    <sys:DateTime>1/13/2009</sys:DateTime>
    <sys:DateTime>1/15/2009</sys:DateTime>
    <sys:DateTime>1/27/2009</sys:DateTime>
    <sys:DateTime>4/2/2009</sys:DateTime>
  </Calendar.SelectedDates>
</Calendar>

注解

日期可以单独添加到集合,也可以使用 方法在一个范围内 AddRange 添加。 根据 属性的值 SelectionMode ,向集合添加日期或范围可能会导致它被清除。 下表列出了对 CalendarSelectionMode 属性的影响 SelectedDates

CalendarSelectionMode 说明
None 不允许选定内容。 SelectedDate 无法设置,并且不能将任何值添加到 SelectedDates
SingleDate 通过设置 SelectedDate 或 中的 SelectedDates第一个值,只能选择单个日期。 无法使用 AddRange
SingleRange 只能选择一个日期范围。 设置 SelectedDate,将日期单独添加到 SelectedDates,或使用 AddRange 将清除 中的所有 SelectedDates以前的值。
MultipleRange 可以选择多个非连续范围的日期。 单独向 SelectedDates 或使用 AddRange 添加日期不会清除 SelectedDates。 设置 SelectedDate 仍将清除 SelectedDates,但随后可以添加其他日期或范围。 添加一个范围,其中包含一些已选择的日期或与另一个范围重叠的区域会导致范围并集,并且不会导致异常。

XAML 属性元素用法

<object>  
  <object.SelectedDates>  
    oneOrMoreDateTimeObjects  
  </object.SelectedDates>  
</object>  

XAML 值

oneOrMoreDateTimeObjects
一个或多个 DateTime 对象元素。

适用于