Constraining scope of context menu

5 posts, 1 answers
  1. Claire
    Claire avatar
    24 posts
    Member since:
    Apr 2015

    Posted 05 Nov 2015 Link to this post

    When I right-click outside the area containing the slots,for example, on the header, the context menu that I have defined still appears provided that I have a selected slot or appointment. Is it possible to constrain the context menu to the slot area?
  2. Nasko
    Admin
    Nasko avatar
    587 posts

    Posted 09 Nov 2015 Link to this post

    Hi Claire,

    In order to achieve the desired behavior of RadContextMenu you need to handle the Opening event. Inside it you need to check if the clicked UIElement is a RadTransitionControl - the Slots and Appointments are placed inside such RadTransitionColtrol. If so, the Handled property should be set to True and thus the ContextMenu wont'be visualized:
    private void RadContextMenu_Opening(object sender, Telerik.Windows.RadRoutedEventArgs e)
    {
        var slot = (sender as RadContextMenu).GetClickedElement<RadTransitionControl>();
     
        if (slot == null)
        {
            e.Handled = true;
        }
    }

    Please, give a try to the proposed above approach and let us know if it worked for you.

    Hope this helps.

    Regards,
    Nasko
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Claire
    Claire avatar
    24 posts
    Member since:
    Apr 2015

    Posted 09 Nov 2015 in reply to Nasko Link to this post

    That's definitely much better.

    The context menu still appears, however, if I right-click on a resource header, the week day header for example.

  5. Answer
    Nasko
    Admin
    Nasko avatar
    587 posts

    Posted 12 Nov 2015 Link to this post

    Hello Claire,

    The observed behavior is an expected one as RadTransitionControl contains the GroupHeaders as well. So, in order to restrict the Contextmenu from appearing even when the GroupHeader gets clicked you could use the following implementation:
    private void RadContextMenu_Opening(object sender, Telerik.Windows.RadRoutedEventArgs e)
    {
        var slot = (sender as RadContextMenu).GetClickedElement<Grid>();
     
        if(slot.Parent != null)
        {
            if (!(slot != null && slot.Parent.GetType() == typeof(RadTransitionControl)))
            {
                e.Handled = true;
            }
        }
        else
        {
            e.Handled = true;
        }
    }

    You might still need to improve the logic if you want to prevent the ContextMenu from appearing when other UI elements get clicked - the approach is the same to use the GetClickedElement and based on it and its children to show the ContextMenu.

    We hope this will help you.

    Regards,
    Nasko
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  6. Claire
    Claire avatar
    24 posts
    Member since:
    Apr 2015

    Posted 12 Nov 2015 in reply to Nasko Link to this post

    Your proposed solution did prevent the context menu from popping up when right-clicking on a resource header, but also when right-clicking on an appointment, which is not desirable.

    So I rewrote it as follows:

    private void RadContextMenu_Opening(object sender, Telerik.Windows.RadRoutedEventArgs e)
    {
        var app = (sender as RadContextMenu).GetClickedElement<AppointmentItem>();
        if (app != null)
        {
            return;
        }
     
        var slot = (sender as RadContextMenu).GetClickedElement<Grid>();
        if (slot == null || slot.Parent == null)
        {
            e.Handled = true;
            return;
        }
     
        if (!(slot.Parent.GetType() == typeof(RadTransitionControl)))
        {
            e.Handled = true;
        }
    }

    This way, the context menu will appear only when the user right-click on a slot, not matter whether the slot is empty or not.

Back to Top
UI for WPF is Visual Studio 2017 Ready