Grubl3r Grubl3r - 2 months ago 80x
C# Question

WPF calendar data binding selects wrong date

I am using a regular WPF Calendar (System.Windows.Controls.Calendar) in an MVVM application. The calendar shows a month at a time, and the user will pick a single day. The calendar shows a few days of the next month at the bottom, which is fine.

The problem is that if I pick a date from the next month (eg the 2nd of the next month) then Calendar first updates the data binding with this date, and then immediately updates it again with the 1st of the month!

This happens for all dates in the next month that I select.

The calendar's SelectedDate and DisplayDate properties are data bound to the view model's Date property.

<Calendar SelectedDate="{Binding Date}" DisplayDate="{Binding Date}" />

The view model looks like this

public class CalendarProblemDemoViewModel : INotifyPropertyChanged
private DateTime _date;

public CalendarProblemDemoViewModel()
_date = DateTime.Today;

public DateTime Date
get { return _date; }
_date = value;
Debug.Write($"new date {_date}\n");
// Note: other code omitted, see the github project

I don't understand why this is happening. I've created a repos on GitHub where you can download an minimal example project. It's made in VS 2015 for .NET 4.5.1:

GitHub repository


You should not bind both SelectedDate and DisplayDate to the same property Date.