我正在尝试在uwp app中使用CalendarDatePicker
控件。我试图将日期限制为MinDate
& maxDate
。我可以在c#中设置如下
Calendercontrol.MinDate=DateTime.Now();
Calendercontrol.MaxDate=DateTime.Now.AddYears(3);
是否可以告诉我如何在Xaml中设置min
和max
值。
答案 0 :(得分: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=""/>