UWP:如何在 xaml 中为 CalendarDatePicker 控件设置 Mindate 和 MaxDate

UWP : How to Set Mindate and MaxDate for CalendarDatePicker Control in xaml

我正在尝试在 uwp 应用程序中使用 CalendarDatePicker 控件。我试图将日期限制为 MinDate & maxDate。我可以像下面这样在 C# 中设置

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

能否告诉我如何在 Xaml 中设置 minmax 值。

创建一个 class 继承自 CalendarDatePicker,添加自定义 min/max 依赖项。

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=""/>