2

I have 2 calendars which I want to show this month and next one, but both with same selected date. I use 2 simple properties ShownDate in my model to store and calculate current and next month.

<Calendar SelectedDate="{Binding Date, Mode=TwoWay}" 
          DisplayDate="{Binding ShownDate, Mode=TwoWay}"
          Margin="4" AllowDrop="True" />
<Calendar SelectedDate="{Binding Date, Mode=TwoWay}" 
          DisplayDate="{Binding ShownDate2, Mode=TwoWay}"
          Margin="4" AllowDrop="True" />

I realized that before showing it, I get a propagation to my model in the ShownDate2 property setting current month (UpdateSource), so forgotting my default values (it must be next month, not current). And it happens before any query for my value (UpdateTarget occurs later).

Is this a bug in Calendar.DisplayDate binding behavior?

Note that all this is contained in a DataTemplate being drawn by a ContentPresenter, but I think it doesn't matter.

UPDATE: Now I'm sure DataTemplates do matter, but can't reproduce the bug in a simple project. I'm still lost.

Pablonete
  • 1,504
  • 1
  • 14
  • 11

1 Answers1

1

I believe this might be a duplicate of WPF: binding viewmodel property of type DateTime to Calendar inside ItemsControl, but to summerize:

The issue appears to be with how the Calendar initializes the DisplayDate property. It currently does it like this:

public Calendar() {
    // ...
    base.SetCurrentValueInternal(DisplayDateProperty, DateTime.Today);
}

It appears that even though the DisplayDate is being initialized before the binding is established, it will still be pushed back to the binding source as if it were set after. This is most likely a bug.

You can work around it using something like:

public class MyCalendar : Calendar {
    public MyCalendar() {
        this.ClearValue(DisplayDateProperty);
    }
}

Or you could establish the binding at a later time (i.e. when the Calendar is loaded).

Community
  • 1
  • 1
CodeNaked
  • 40,753
  • 6
  • 122
  • 148