0
votes

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; }
        set
        {
            _date = value;
            Debug.Write($"new date {_date}\n");
            OnPropertyChanged(nameof(Date));
        }
    }
    // Note: other code omitted, see the github project
}

I don't understand why this is happening?

1
So you bound both to the same property Date. Did you try and see if SelectedDate is different than DisplayDate? - Khalil Khalaf
That actually solves the problem LOL! So it has to do with the inner workings of Calendar? I think I'll fire up the disassembler and have a look. - Grubl3r
Glad we were able to help :) - Khalil Khalaf

1 Answers

1
votes

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