Can't get the CalendarDatePicker in UWP behaviour right in MVVM

Viewed 1834

The UWP app is dead simple. A ViewModel with a property which is bound by a Page containing a CalendarDatePicker.

View model:

public class MainPageViewModel : INotifyPropertyChanged
{
    private DateTimeOffset date;

    public MainPageViewModel()
    {

    }

    public event PropertyChangedEventHandler PropertyChanged;

    public DateTimeOffset Date
    {
        get { return date; }
        set
        {
            date = value; 
            OnPropertyChanged();
        }
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

Page:

<Page
x:Class="App2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.DataContext>
    <local:MainPageViewModel />
</Page.DataContext>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <CalendarDatePicker />
</Grid>
</Page>

I run this application and CalendarDatePicker behaves as expected. Starts up with no date selected, shows verbiage "select a date", when expanded defaults to today's date.

The moment I bind the CalendarDatePicker Date property to my vm's Date and run the app, the date defaults to April 16, 1916.

I tried making Date nullable, changing binding mode to TwoWay and numerous other things to no avail.

Has anyone experienced the same issues? How to work with this control in MVVM?

2 Answers
Related