Dbloom
Dbloom

Reputation: 1402

How can I determine which date the mouse is over in a WPF Calendar control?

I have a Calendar control in my WPF app. I want to display a message based on whichever date the user hovers the mouse over.

I think that the Calendar control uses a button for each date, and that button has its DataContext set to a DateTime object.

But how can I use the MouseMove event of the calendar to see which date the mouse is currently over?

Upvotes: 2

Views: 317

Answers (1)

Xiaoy312
Xiaoy312

Reputation: 14477

You can use Mouse.DirectlyOver to get the element directly under your mouse, and then find the date via:

calendar.MouseMove += (s, e) =>
{
    if (Mouse.DirectlyOver is FrameworkElement el && 
        el.TemplatedParent is CalendarDayButton button && 
        el.DataContext is DateTime date)
    {
        // do stuff with `date`...
    }
};

Upvotes: 2

Related Questions