Home > Back-end >  Display PlaceholderText When DateTime is null in CalendarPicker in UWP XAML
Display PlaceholderText When DateTime is null in CalendarPicker in UWP XAML

Time:10-20

I have a CalendarDatePicker with some DateTime object bound to it's Date property. When the bound Date is null, The picker is showing the least date available (1/1/1922). How can I make the picker just show the PlaceHolderText when the bound Date is null? The FuzzyDateConverter is just a simple converter which converts individual day, month and year to DateTime Object...

The CalendarPicker is Below.

<CalendarDatePicker 
    PlaceholderText="Pick a date"
    HorizontalAlignment="Stretch"
    DateFormat="{}{day.integer}/{month.integer}/{year.full}"
    LightDismissOverlayMode="On"
    Date="{x:Bind ViewModel.CompletedAt, Converter={StaticResource FuzzyDateConverter}}"/>

And the Converter is below...

public class FuzzyToDateTimeObject : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return new DateTimeOffset(((DateTime)value).ToUniversalTime());
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return ((DateTimeOffset)value).DateTime;
    }
}

CodePudding user response:

I tried your code and it shows the "Pick a date" placeholder when CompletedAt is null.

My first guess is that your FuzzyDateConverter is not returning a null, or that CompletedAt is not a nullable.

  • Related