检测Silverlight日历控件上何时单击一天 [英] Detecting when a day is clicked on the Silverlight Calendar control

查看:199
本文介绍了检测Silverlight日历控件上何时单击一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing something that will require similar functionality to the Silverlight DatePicker - a popup containing a Calendar control will be shown, and after the user clicks a date, or uses the keyboard to select a date and presses enter/space, the popup should close.

我可以显示日历很好,但我失去了当用户点击一天或按enter / space。 SelectedDatesChanged 事件不会指示用户是否点击了所选日期,或者是否刚刚通过键盘传递。

I can show the calender just fine, but I am at a loss at figuring out when the user has clicked a day or pressed enter/space. The SelectedDatesChanged event does not give any indication of whether the user clicked the selected date, or was just passing over it with the keyboard.

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

Reflector shows that the DatePicker control is cheating, using an internal DayButtonMouseUp event on the Calendar control.

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

Does anyone know a solution to this problem?

推荐答案

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

Not a very clean solution. Also, it does not properly take into account BlackoutDates because the IsBlackOut property of the button is also internal. I could manually check against that in my click event, but for my purposes I didn't need to support that.

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/

FindVisualChildByName is copied from http://pwnedcode.wordpress.com/2009/04/01/find-a-control-in-a-wpfsilverlight-visual-tree-by-name/

这篇关于检测Silverlight日历控件上何时单击一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆