2016-04-08 3 views
1

Ich versuche CalendarDatePicker Kontrolle in UWP App zu nutzen .Ich versuchte, das Datum zu MinDate & maxDate zu begrenzen. Ich kann in der Lage, wie in C# setzen unterUWP: Wie MinDate und MaxDate für CalendarDatePicker Control Set in XAML

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

Können Sie bitte lassen Sie mich wissen, wie min und max Wert in XAML festlegen.

Antwort

0

Erstellen Sie eine von CalendarDatePicker geerbte Klasse, fügen Sie eine benutzerdefinierte Min/Max-Abhängigkeit hinzu.

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; 
    } 
} 

Verbrauch:

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

Hallo, Dank Ihrer Antwort. Ich habe das selbe versucht, aber ich bekomme folgenden Fehler Kann nicht Textwert "10/2/2017" in Eigenschaft Min des Typs DateTimeOfffset zuweisen. xaml -

+1

Ich habe den Min- und Max-Eigenschaftstyp DateTimeOffset in string geändert. Jetzt funktioniert es Dank –

+0

Froh, dass es für Sie funktioniert hat, akzeptieren Sie bitte als Antwort pls :) – thang2410199

Verwandte Themen