2016-04-08 12 views
1

にCalendarDatePickerコントロールのためのMinDateプロパティとmaxDateのを設定するにはどうすれば.IはMinDate & maxDateに日付を制限しようとしたUWPアプリでCalendarDatePickerコントロールを使用しようとしています。私はUWPは:XAML

Calendercontrol.MinDate=DateTime.Now(); 
Calendercontrol.MaxDate=DateTime.Now.AddYears(3); 

以下のようにC#で設定することができますが、私は、XAMLで minmax値を設定する方法を教えてくださいことができます。

答えて

0

CalendarDatePickerから継承したクラスを作成し、カスタムの最小/最大の依存関係を追加します。

public class CustomCalendarDatePicker : CalendarDatePicker 
{ 
    public DateTimeOffset Max 
    { 
     get { return (DateTimeOffset)GetValue(MaxProperty); } 
     set { SetValue(MaxProperty, value); } 
    } 

    public static readonly DependencyProperty MaxProperty = 
     DependencyProperty.Register(
      nameof(Max),      // The name of the DependencyProperty 
      typeof(DateTimeOffset),     // The type of the DependencyProperty 
      typeof(CustomCalendarDatePicker), // The type of the owner of the DependencyProperty 
      new PropertyMetadata(   
        null, onMaxChanged     // The default value of the DependencyProperty 
      )); 

    private static void onMaxChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var calendar = d as CustomCalendarDatePicker; 
     calendar.MaxDate = (DateTimeOffset)e.NewValue; 
    } 

    public DateTimeOffset Min 
    { 
     get { return (DateTimeOffset)GetValue(MinProperty); } 
     set { SetValue(MinProperty, value); } 
    } 

    public static readonly DependencyProperty MinProperty = 
     DependencyProperty.Register(
      nameof(Min),      // The name of the DependencyProperty 
      typeof(DateTimeOffset),     // The type of the DependencyProperty 
      typeof(CustomCalendarDatePicker), // The type of the owner of the DependencyProperty 
      new PropertyMetadata(   
       null, onMinChanged      // The default value of the DependencyProperty 
      )); 

    private static void onMinChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var calendar = d as CustomCalendarDatePicker; 
     calendar.MinDate = (DateTimeOffset)e.NewValue; 
    } 
} 

使用法:

<controls:CustomCalendarDatePicker Min="" Max=""/> 
+0

こんにちは、おかげでお返事。私は同じことを試みましたが、次のエラーが発生しています。テキスト値 "10/2/2017"をDateTimeOfffset型のプロパティMinに割り当てることができません。 xaml -

+1

MinとMaxのプロパティの型DateTimeOffsetを文字列に変更しました –

+0

うまくいきました。 pls :) – thang2410199

関連する問題