如何检测鼠标双击日历限制日期?



在我的 WPF/C# 应用中,我有一个Calendar控件,设置了一系列BlackoutDates。我还想处理MouseDoubleClick事件,但我看不到如何确定用户已双击中断日期 - 在这种情况下,返回到事件处理程序的日期是最近选择的有效(即非中断日期(日期。 如何"忽略"双击这些BlackoutDates

编辑:XAML:

<Calendar MouseDoubleClick="Calendar_MouseDoubleClick"/>

代码隐藏:

private void Calendar_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (myCalendarBlackoutDatesCollection.Contains(/* what goes here? */))
{
return;  // ignore doubleclick
}
// execution continues here
}

您可以通过定义一个CalendarDayButtonStyle来处理每个CalendarDayButtonMouseDoubleClick

<Calendar x:Name="cal">
<Calendar.CalendarDayButtonStyle>
<Style TargetType="CalendarDayButton">
<EventSetter Event="MouseDoubleClick" Handler="cal_MouseDoubleClick" />
</Style>
</Calendar.CalendarDayButtonStyle>
</Calendar>

private void cal_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
System.Windows.Controls.Primitives.CalendarDayButton button = sender as System.Windows.Controls.Primitives.CalendarDayButton;
DateTime clickedDate = (DateTime)button.DataContext;
if (!cal.BlackoutDates.Contains(clickedDate))
{
MessageBox.Show("No blackout date was clicked!");
}
}

最新更新