【问题标题】:Detecting when a day is clicked on the Silverlight Calendar control检测何时在 Silverlight 日历控件上单击一天
【发布时间】:2010-11-11 19:48:05
【问题描述】:

我正在开发一些需要与 Silverlight DatePicker 类似功能的东西 - 将显示一个包含日历控件的弹出窗口,在用户单击日期或使用键盘选择日期并按 Enter/空格键后,弹出窗口应该关闭。

我可以很好地显示日历,但我不知道用户何时单击了一天或按下了输入/空格。 SelectedDatesChanged 事件没有给出任何指示,表明用户是点击了选定的日期,还是只是用键盘跳过了它。

Reflector 显示 DatePicker 控件在作弊,使用 Calendar 控件上的内部 DayButtonMouseUp 事件。

有人知道这个问题的解决方案吗?

【问题讨论】:

    标签: silverlight calendar


    【解决方案1】:

    您可以通过将 DayButtons 的 ClickMode 设置为“Hover”来实现此目的。 在此之后,您可以访问 MouseLeftButtonDown 事件

        <sdk:Calendar Name="calendar1" MouseLeftButtonDown="calendar1_MouseLeftButtonDown">
            <sdk:Calendar.CalendarDayButtonStyle>
                <Style TargetType="Primitives:CalendarDayButton">
                    <Setter Property="ClickMode" Value="Hover"/>
                </Style>
            </sdk:Calendar.CalendarDayButtonStyle>
        </sdk:Calendar>
    

    【讨论】:

      【解决方案2】:

      不是一个非常干净的解决方案。此外,它没有正确考虑 BlackoutDates,因为按钮的 IsBlackOut 属性也是内部的。我可以在我的点击事件中手动检查它,但出于我的目的,我不需要支持它。

      void CalendarControl_Loaded(object sender, RoutedEventArgs e)
      {
          var grid = FindVisualChildByName<Grid>(CalendarControl, "MonthView");
          // Loaded may be called several times before both the grid and day buttons are created
          if (grid != null && grid.Children.OfType<System.Windows.Controls.Primitives.CalendarDayButton>().Any()) 
          {
              // Add our own click event directly to the button
              foreach (var button in grid.Children.OfType<System.Windows.Controls.Primitives.CalendarDayButton>().Cast<System.Windows.Controls.Primitives.CalendarDayButton>())
              {
                  button.Click += new RoutedEventHandler(button_Click);
              }
              // We only want to add the event once
              CalendarControl.Loaded -= new RoutedEventHandler(CalendarControl_Loaded);
          }
      }
      
      void button_Click(object sender, RoutedEventArgs e)
      {
          var button = (System.Windows.Controls.Primitives.CalendarDayButton)sender;
          var date = button.DataContext as DateTime?;
          // The user clicked a date. Close the calendar and do something with it
      }
      

      FindVisualChildByName 复制自 http://pwnedcode.wordpress.com/2009/04/01/find-a-control-in-a-wpfsilverlight-visual-tree-by-name/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-01
        相关资源
        最近更新 更多