2012-09-25 23 views

回答

1

有可從Telerik一個datepicker。但它不是免費的。

+0

雖然此鏈接可以回答這個問題,最好是在這裏有答案的主要部件,並提供鏈接以供參考。如果鏈接頁面更改,則僅鏈接答案可能會失效。 –

0

通過這個Link,我認爲這是你在找什麼。

而在此之前,你應該必須安裝telerik免費windows8的RC

2

我發現這個從CodePlex上的私人解決方案,我真誠道歉給誰就給誰擁有的鏈接...但你可以創建一個控制下面的代碼:

XAML文件:

<UserControl x:Class="MyNamespace.Controls.DatePicker" 
    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"> 

    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 
     <Grid x:Name="LayoutRoot" Background="Transparent"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto" /> 
       <ColumnDefinition Width="Auto" /> 
       <ColumnDefinition Width="Auto" /> 
      </Grid.ColumnDefinitions> 

      <ComboBox x:Name="Day" Grid.Column="0" SelectionChanged="Day_SelectionChanged" Width="70" Margin="0,0,10,0" /> 
      <ComboBox x:Name="Month" Grid.Column="1" SelectionChanged="Day_SelectionChanged" Width="70" Margin="0,0,10,0" /> 
      <ComboBox x:Name="Year" Grid.Column="2" SelectionChanged="Day_SelectionChanged" Width="100" /> 
     </Grid> 
    </Grid> 
</UserControl> 

,並落後於它的代碼了:

using System; 
using System.Collections.Generic; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 

namespace MyNamespace.Controls 
{ 
    public sealed partial class DatePicker 
    { 
     private bool initializing = true; 
     public DatePicker() 
     { 
      InitializeComponent(); 

      UpdateValues(0, 0); 
      Day.SelectedIndex = 0; 
      Month.SelectedIndex = 0; 
      Year.SelectedIndex = 0; 
      initializing = false; 
     } 

     public static readonly DependencyProperty AllowNullProperty = 
      DependencyProperty.Register("AllowNull", typeof(bool), typeof(DatePicker), new PropertyMetadata(true)); 

     public bool AllowNull 
     { 
      get { return (bool)GetValue(AllowNullProperty); } 
      set { SetValue(AllowNullProperty, value); } 
     } 

     public static readonly DependencyProperty SelectedDateProperty = 
      DependencyProperty.Register("SelectedDate", typeof(DateTime?), typeof(DatePicker), new PropertyMetadata(null, OnSelectedItemChanged)); 

     public DateTime? SelectedDate 
     { 
      get { return (DateTime?)GetValue(SelectedDateProperty); } 
      set { SetValue(SelectedDateProperty, value); } 
     } 

     public event RoutedEventHandler SelectedItemChanged; 

     private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      var ctrl = (DatePicker)d; 
      if (ctrl.initializing) 
       return; 

      ctrl.initializing = true; 
      ctrl.UpdateDate(); 
      ctrl.initializing = false; 

      if (ctrl.SelectedItemChanged != null) 
       ctrl.SelectedItemChanged(ctrl, new RoutedEventArgs()); 
     } 

     public void UpdateDate() 
     { 
      if (SelectedDate.HasValue) 
      { 
       UpdateValues(SelectedDate.Value.Year, SelectedDate.Value.Month); 

       if (AllowNull) 
       { 
        Day.SelectedIndex = SelectedDate.Value.Day; 
        Month.SelectedIndex = SelectedDate.Value.Month; 
        Year.SelectedIndex = SelectedDate.Value.Year - 2000 + 1; 
       } 
       else 
       { 
        Day.SelectedIndex = SelectedDate.Value.Day - 1; 
        Month.SelectedIndex = SelectedDate.Value.Month - 1; 
        Year.SelectedIndex = SelectedDate.Value.Year - 2000; 
       } 
      } 
      else 
      { 
       UpdateValues(0, 0); 
       Day.SelectedIndex = 0; 
       Month.SelectedIndex = 0; 
       Year.SelectedIndex = 0; 
      } 
     } 

     public void UpdateValues(int year, int month) 
     { 
      var days = new List<string>(); 
      if (AllowNull) 
       days.Add(" "); 
      for (var i = 1; i <= 31; i++)//(year != 0 && month != 0 ? DateTime.DaysInMonth(year, month) : 31); i++) 
       days.Add(i.ToString()); 

      var months = new List<string>(); 
      if (AllowNull) 
       months.Add(" "); 
      for (var i = 1; i <= 12; i++) 
       months.Add(i.ToString()); 

      var years = new List<string>(); 
      if (AllowNull) 
       years.Add(" "); 
      for (var i = 2000; i <= 2020; i++) 
       years.Add(i.ToString()); 

      if (Month.SelectedIndex > months.Count - 1) 
       Month.SelectedIndex = months.Count - 1; 

      Day.ItemsSource = days; 
      Month.ItemsSource = months; 
      Year.ItemsSource = years; 
     } 

     private void Day_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      if (initializing) 
       return; 

      var hour = SelectedDate != null ? SelectedDate.Value.Hour : 0; 
      var minute = SelectedDate != null ? SelectedDate.Value.Minute : 0; 
      var second = SelectedDate != null ? SelectedDate.Value.Second : 0; 

      initializing = true; 
      if (AllowNull && (Day.SelectedIndex == 0 || Month.SelectedIndex == 0 || Year.SelectedIndex == 0)) 
       SelectedDate = null; 
      else 
      { 
       if (AllowNull) 
        SelectedDate = new DateTime(Year.SelectedIndex + 2000 - 1, Month.SelectedIndex, Day.SelectedIndex, hour, minute, second); 
       else 
        SelectedDate = new DateTime(Year.SelectedIndex + 2000, Month.SelectedIndex + 1, Day.SelectedIndex + 1, hour, minute, second); 
      } 

      //if (SelectedItem.HasValue) 
      // UpdateValues(SelectedItem.Value.Year, SelectedItem.Value.Month); 
      //else 
      // UpdateValues(0, 0); 

      initializing = false; 
     } 
    } 
} 
相關問題