xamarin 如何创建可为空的DatePickerControl?

w3nuxt5m  于 2022-12-31  发布在  其他
关注(0)|答案(2)|浏览(162)

我尝试在我的应用程序中使用.NET MAUI的日期选择器控件。我有一个要求,最初当屏幕显示需要显示什么或“-”下面的开始日期标签。但日期选择器控件显示默认值,如01/01/1900。
现在,问题是如何修改一个日期选择器控件,使其最初不显示任何默认值?
我已经做了它使用标签控件上面的日期选择器内网格。但这是工作与不同的控制。如何处理这个使用日期选择器控件本身?这是可能的吗?

63lcw9qa

63lcw9qa1#

也许像这样。

public class MyDatePicker : DatePicker
{
    private string _format = null;
    public static readonly BindableProperty NullableDateProperty = BindableProperty.Create<MyDatePicker, DateTime?>(p => p.NullableDate, null);
    public DateTime? NullableDate
    {
        get { return (DateTime?)GetValue(NullableDateProperty); }
        set { SetValue(NullableDateProperty, value); UpdateDate(); }
    }
    private void UpdateDate()
    {
        if (NullableDate.HasValue) { if (null != _format) Format = _format; Date = NullableDate.Value; }
        else { _format = Format; Format = "pick ..."; }
    }
    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();
        UpdateDate();
    }
    protected override void OnPropertyChanged(string propertyName = null)
    {
        base.OnPropertyChanged(propertyName);
        if (propertyName == "Date") NullableDate = Date;
    }
}

在Xaml中

<local:MyDatePicker x:Name="myDatePicker"></local:MyDatePicker>

在主页. cs中

{
        InitializeComponent();
        myDatePicker.NullableDate = null;
    }
kkbh8khc

kkbh8khc2#

在Maui,DatePicker字段可以为空,这是一个需要增强的功能,有人在GitHub上提交了申请,你可以关注一下:[Enhancement] DatePicker and TimePicker should support DateOnly? and TimeOnly?

相关问题