如何确定鼠标在 WPF 日历控件中停留的日期?
How can I determine which date the mouse is over in a WPF Calendar control?
我的 WPF 应用程序中有一个日历控件。我想根据用户将鼠标悬停在哪个日期显示一条消息。
我认为日历控件为每个日期使用一个按钮,并且该按钮将其 DataContext 设置为 DateTime 对象。
但是如何使用日历的 MouseMove 事件来查看鼠标当前停留在哪一天呢?
您可以使用Mouse.DirectlyOver
获取鼠标正下方的元素,然后通过以下方式查找日期:
calendar.MouseMove += (s, e) =>
{
if (Mouse.DirectlyOver is FrameworkElement el &&
el.TemplatedParent is CalendarDayButton button &&
el.DataContext is DateTime date)
{
// do stuff with `date`...
}
};
我的 WPF 应用程序中有一个日历控件。我想根据用户将鼠标悬停在哪个日期显示一条消息。
我认为日历控件为每个日期使用一个按钮,并且该按钮将其 DataContext 设置为 DateTime 对象。
但是如何使用日历的 MouseMove 事件来查看鼠标当前停留在哪一天呢?
您可以使用Mouse.DirectlyOver
获取鼠标正下方的元素,然后通过以下方式查找日期:
calendar.MouseMove += (s, e) =>
{
if (Mouse.DirectlyOver is FrameworkElement el &&
el.TemplatedParent is CalendarDayButton button &&
el.DataContext is DateTime date)
{
// do stuff with `date`...
}
};