How to display a ContextMenu programatically

10 posts, 0 answers
  1. GEB
    GEB avatar
    228 posts
    Member since:
    Dec 2008

    Posted 10 Jan 2009 Link to this post

    I want to create a ContectMenu in my XAML file, but display it from within my code-behind.  That is, I do not want the ContextMenu to capture the event and display itself.  I want to programatically display the ContectMenu.  Any way to do this?

    One recommendation for improvement would be to have the ContextMenu behave like a Popup.  For example:

    myContextMenu.IsOpen = True;

    would cause the menu to be displayed, and

    myContextMenu.IsOpen = False;

    would cause it to be hidden.

    In this way, the behavior could alternatively be to display/hide without the ContextMenu capturing any events.  This would also allow any ContextMenu to be displayed via a single event based on the current state of the application.
  2. Tihomir Petkov
    Admin
    Tihomir Petkov avatar
    576 posts

    Posted 12 Jan 2009 Link to this post

    Hello Gary,

    What you can do in your scenario is handle the event for which the context menu is listening in the cases when you don't want it to be displayed, i.e. you can do the following:

    <

     

    nav:RadTreeView x:Name="treeView" Selected="treeView_Selected">
        <nav:RadContextMenu.ContextMenu>
            <nav:RadContextMenu x:Name="contextMenu" EventName="Selected">

     

    That way you can implement the logic which determines whether the context menu should be displayed in the in the treeView_Selected method. I am attaching a sample project which demonstrates what I am suggesting. Please let me know if this solves your issue.

    Kind regards,
    Tihomir Petkov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. DevCraft banner
  4. GEB
    GEB avatar
    228 posts
    Member since:
    Dec 2008

    Posted 12 Jan 2009 Link to this post

    Thank you for your quick response.  howeverr, I have a follow up question.  I am able to capture the event, just as you do in your sample.  However, when I capture the event, what method do I use to cause the context menu to be displayed?  Note that the control that is generating the event is another third-party event, as opposed to a RadTreeView object. 

    In your sample, you have:

    RadTreeView tree = sender

    as RadTreeView;

     

     

    if ((e.OriginalSource as RadTreeViewItem).Header.ToString() != "Item 2")

     

    {

          e.Handled =

    true;

     

    }

     

    else

     

    {
          // What do I do here to get the ContectMenu to pop up?  It is NOT being displayed automatically.

    }

    Within this method, what calls do I make to show the contect menu?

  5. Tihomir Petkov
    Admin
    Tihomir Petkov avatar
    576 posts

    Posted 12 Jan 2009 Link to this post

    Hi Gary,

    It is strange that the context menu doesn't show up when you run the example I sent you - it does on my machine. My guess would be that the third-party control you use does not fire a Selected event and you should specify an event that is fired by the control you use. Anyways, if the context menu doesn't show up automatically you can set its IsOpen property to true like so:

    contextMenu

     

    .IsOpen = true;

    Hope that helps.

    Kind regards,
    Tihomir Petkov
    the Telerik team


    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. GEB
    GEB avatar
    228 posts
    Member since:
    Dec 2008

    Posted 12 Jan 2009 Link to this post

    Actually, it is even stranger.  I can see the event fired from the 3rd party control.  But, when I try to access the ContextMenu programatically in the code-behind event handler by the name of the ContextMenu, it compiles fine, but generates a run-time error, indicating that the ContextMenu is NULL.  For example, borderContextMenu.IsOpen = true; generates a runtime error indicating that borderContextMenu is NULL, and that I should use the "new" directive.
  7. Tihomir Petkov
    Admin
    Tihomir Petkov avatar
    576 posts

    Posted 12 Jan 2009 Link to this post

    Hello Gary,

    The problem must be in the third-party control you use. Here is some theory: in order for the x:Name of a control to work, that control should be placed in the ContentProperty of the control it's placed in. Currently in Silverlight if you set the ContentProperty of a custom control this wouldn't work due to a bug in the plugin. So, if the third-party control you use is not based on say ContentControl or ItemsControl, this may be why the context menu is null - it cannot be found by its x:Name. It may sound a bit complicated but that would be my guess. If you were using one of our controls I could investigate further, but there is nothing I can do with a third-party control.

    Greetings,
    Tihomir Petkov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  8. GEB
    GEB avatar
    228 posts
    Member since:
    Dec 2008

    Posted 13 Jan 2009 Link to this post

    It appears you were absolutely correct about the ContentControl and ItemsControl.  The vendor of the 3rd party control has indicated that there is a template available that could be used.  It is in the form of:

    <mywidget:AboutTemplate Canvase.Left="10" Canvas.Top="10">
    </mywidget:AboutTemplate>

    I attempted to insert a simple ContextMenu within the template, but it still does not display.  Note that there is no event to trigger off of.  Any recommendations on how to get the ContextMenu to work in a Template environment such as this?
  9. Tihomir Petkov
    Admin
    Tihomir Petkov avatar
    576 posts

    Posted 13 Jan 2009 Link to this post

    Hello Gary,

    Control templates use a different name scope from that of the application, so you won't be able to get the context menu from the template and tell it what to do. It is possible to get elements from the control template in WPF but unfortunatelly this is not available in Silverlight.

    Regards,
    Tihomir Petkov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  10. Ez
    Ez avatar
    5 posts
    Member since:
    Dec 2008

    Posted 19 Mar 2009 Link to this post

    Tihomir ,

    I also need the context menu to only appear in certain circumstances.

    I have followed your instructions laid out in your sample project.
    The context menu is displaying despite my use of the e.Handled command.

    I think the issue is, that the event I'm listening to in the Control in which the context menu is placed is the MouseLeftButtonUp event. Therefor, the context menu's displaying (or not) is not dependant on how the parent control's event is handled.

    In your example the context menu is listening to a unique event that is fired in the Parent Control. (i.e. Selected)

    Thanks for your help in advance,
    ez

  11. Tihomir Petkov
    Admin
    Tihomir Petkov avatar
    576 posts

    Posted 20 Mar 2009 Link to this post

    Hello Ez,

    Can you please elaborate a bit more on the exact scenario you want to implement?

    Best wishes,
    Tihomir Petkov
    the Telerik team


    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top
DevCraft banner