Have to click away twice from Calendar in WPF

后端 未结 7 1717
南旧
南旧 2021-02-01 19:17

Edit 2: Thank you all for your feedback. I solved the problem by adding this to my SelectedDatesChanged event:

Mouse.Capture(null);

When I select a

7条回答
  •  说谎
    说谎 (楼主)
    2021-02-01 19:54

    Releasing all mouse clicks in SelectedDatesChanged or GotMouseCapture will break the navigation between months on the calendar control. As pointed out in another answer, SelectedDatesChanged does not fire when the same date is selected.

    So I used GotMouseCapture and only released the mouse focus when the clicked UIElement was a calendar day. It fixes the focus issue and doesn't break the rest of the control.

    private void Calendar_GotMouseCapture(object sender, MouseEventArgs e)
    {
        UIElement originalElement = e.OriginalSource as UIElement;
        if (originalElement is CalendarDayButton || originalElement is CalendarItem)
        {
            originalElement.ReleaseMouseCapture();
        }
    }
    

提交回复
热议问题