检测何时在 Silverlight 日历控件上单击某一天

发布于 2024-10-02 03:54:02 字数 351 浏览 2 评论 0原文

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

我可以很好地显示日历,但我无法弄清楚用户何时单击了一天或按下了 Enter/空格键。 SelectedDatesChanged 事件不会给出任何指示,表明用户是否单击了所选日期,或者只是用键盘跳过了该日期。

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

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

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.

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 shows that the DatePicker control is cheating, using an internal DayButtonMouseUp event on the Calendar control.

Does anyone know a solution to this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉黛浅 2024-10-09 03:54:02

您可以通过将 DayButtons 的 ClickMode 设置为“悬停”来实现此目的。
之后您可以访问 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>

You can achieve this by setting the ClickMode of the DayButtons to 'Hover'.
After this you can lsiten to the MouseLeftButtonDown-event

    <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>
苯莒 2024-10-09 03:54:02

这不是一个非常干净的解决方案。此外,它没有正确考虑 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/

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 is copied from http://pwnedcode.wordpress.com/2009/04/01/find-a-control-in-a-wpfsilverlight-visual-tree-by-name/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文