29

I am using the WPF Calendar that is part of the WPF Toolkit.

I have two different calendars on a control. When I attempt to choose a date from one calendar and then from the second calendar, I have to click on the second calendar twice to get it to choose a date.

Has anyone else had this issue and know of a solution?

timothymcgrath
  • 1,318
  • 1
  • 9
  • 19
  • Hmm, I never noticed Calendar behaving too weirdly, but I haven't used it too much, and possibly not in the same situation as you. That said, the WPF Toolkit controls aren't really perfect, so this issue is entirely believable. – Benny Jobigan Mar 11 '10 at 15:05
  • 7
    FYI, I am also experiencing this issue with the WPF Calendar within the .NET Framework 4.0 – Luke May 30 '11 at 09:41

2 Answers2

35

The calendar can capture the mouse without a date change (e.g. in CalendarMode drill down). A better solution is this:

protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
{
    base.OnPreviewMouseUp(e);
    if (Mouse.Captured is CalendarItem)
    {
        Mouse.Capture(null);
    }
}
Martin
  • 1,028
  • 17
  • 23
  • 1
    This fixed an issue for me where the selected date range was lost when another control received the mouse up event. – mjcopple Dec 14 '11 at 18:32
  • 2
    I recommend extended version of this code http://stackoverflow.com/questions/5543119/wpf-button-takes-two-clicks-to-fire-click-event – NoWar Jan 19 '15 at 17:24
5

I added this code when changing the SelectedDates of the Calendar and it fixed the issue.

        Private Sub Calendar_SelectedDatesChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles Me.SelectedDatesChanged
        Me.DisplayDate = CType(Me.SelectedDate, DateTime)

        ' This is to prevent the Calendar DayButtons from holding the focus in the Calendar.
        Me.CaptureMouse()
        Me.ReleaseMouseCapture()
    End Sub
timothymcgrath
  • 1,318
  • 1
  • 9
  • 19
  • The Me.DisplayDate line is just to move the view to the SelectedDate, this isn't needed for the fix. – timothymcgrath Mar 22 '10 at 12:54
  • 1
    Thank you! This fixed the issue for me when using the .NET Framework 4.0 Calendar control. It seems that the bug has persisted through to the current final release of .NET 4.0 – Luke May 30 '11 at 09:44