我正在尝试CalendarDatePicker
在 uwp 应用程序中使用控件。我试图将日期限制为MinDate
& maxDate
。我可以在 c# 中设置,如下所示
Calendercontrol.MinDate=DateTime.Now();
Calendercontrol.MaxDate=DateTime.Now.AddYears(3);
您能否让我知道如何在 Xaml中设置min
和赋值。max
我正在尝试CalendarDatePicker
在 uwp 应用程序中使用控件。我试图将日期限制为MinDate
& maxDate
。我可以在 c# 中设置,如下所示
Calendercontrol.MinDate=DateTime.Now();
Calendercontrol.MaxDate=DateTime.Now.AddYears(3);
您能否让我知道如何在 Xaml中设置min
和赋值。max
创建一个继承自 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=""/>