【问题标题】:How to detect MouseDoubleClick on Calendar BlackoutDates?如何检测日历 BlackoutDates 上的 MouseDoubleClick?
【发布时间】:2017-07-20 23:03:20
【问题描述】:

在我的 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
}

【问题讨论】:

  • 我们可以看看一些代码吗?
  • @MattL。不知道这会有什么帮助,但我会添加一些东西

标签: .net wpf calendar double-click


【解决方案1】:

您可以通过定义CalendarDayButtonStyle 来处理每个CalendarDayButton 的MouseDoubleClick:

<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!");
    }
}

【讨论】:

  • 这太棒了!但是......我也在使用Interaction.Triggers 用于 MVVM 模式,并且没有弄清楚如何有效地将处理程序移动到视图模型,而不是将它放在代码隐藏中。建议?还是我应该提出另一个问题
  • 是的,请。不要在 cmets 字段中提出其他问题。
  • 但是给你一个引导;编写一个调用命令的附加行为。或者简单地从视图中的代码隐藏事件处理程序调用命令。您不能在样式中应用交互触发器。
猜你喜欢
  • 1970-01-01
  • 2016-04-25
  • 1970-01-01
  • 1970-01-01
  • 2011-08-15
  • 2016-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多