c#右键更改SelectedDate并显示ContextMenu

本文关键字:显示 ContextMenu SelectedDate 右键 | 更新日期: 2023-09-27 18:15:43

我想在RadCalendar中实现右键事件以更改SelectedDates并同时显示ContextMenu。我使用以下代码:

XAML

<telerik:RadCalendar Name="radCalendar"
                     Canvas.Left="80"
                     Canvas.Top="200"
                     Height="320"
                     Width="400"
                     SelectedDate="{Binding CurrentDate, ElementName=radScheduleView, Mode=TwoWay}"
                     SelectionMode="Single"
                     DisplayDate="{Binding DisplayDate, Mode=TwoWay}">
    <telerik:RadContextMenu.ContextMenu>
        <telerik:RadContextMenu Opened="RadContextMenu_Opened">
            <telerik:RadMenuItem x:Name="expandOverview"
                                 Header="Expand Overview"
                                 Click="expandOverview_Click" />
            <telerik:RadMenuItem x:Name="showLayouts"
                                 Header="Show Layouts"
                                 Click="showLayouts_Click" />
        </telerik:RadContextMenu>
    </telerik:RadContextMenu.ContextMenu>
</telerik:RadCalendar>

c

private void RadContextMenu_Opened(object sender, RoutedEventArgs e)
{
    var calendarButton = (sender as RadContextMenu).GetClickedElement<CalendarButton>();
    if (calendarButton != null)
    {
        var calendarButtonContent = calendarButton.Content as CalendarButtonContent;
        if (calendarButtonContent != null)
        {
            var clickedDate = calendarButtonContent.Date;
            //radCalendar.SelectedDate = calendarButtonContent.Date;
        }
    }
}

如果我删除radCalendar.SelectedDate = calendarButtonContent.Date;中的评论,我可以通过右键单击来选择日期,但我需要再次右键单击以显示ContextMenu,如果我将其保留在评论中,ContextMenu将显示,但我不能用右键单击更改SelectedDates

c#右键更改SelectedDate并显示ContextMenu

已解决

不使用open,而是使用open,并添加了处理过的属性

private void RadContextMenu_Opening(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
    var calendarButton = (sender as RadContextMenu).GetClickedElement<CalendarButton>();
    if (calendarButton != null && (calendarButton.ButtonType == CalendarButtonType.Date || calendarButton.ButtonType == CalendarButtonType.TodayDate))
    {
        var calendarButtonContent = calendarButton.Content as CalendarButtonContent;
        if (calendarButtonContent != null)
        {
            var clickedDate = calendarButtonContent.Date;
            radCalendar.SelectedDate = calendarButtonContent.Date;
        }
    }
    else
    {
        e.Handled = true;
    }
}