Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1115
Can you cause the menu to appear in code behind
posted

My application is that instead of having the menu be preset, I want to define it in code behind based on where the user clicked, and then make it appear automatically, not based on a mouse event.

The way I capture the click current is

MouseRightButtonDown

 

 

= in the Xamgrid definition.

The reason why I don't want to just define the menu in advance, is that I do things like look at the contents of the cell and if it has a URL in it, I offer opening the page as one of my menu selections.

Parents
  • 3071
    Suggested Answer
    posted

    Hi,
    This sample code shows how to create and open the context menu in code behind:
    private void txtLog_TextChanged(object sender, TextChangedEventArgs e)
    {
      TextBox element = sender as TextBox;
      if(string.IsNullOrEmpty(element.Text))
        return;

      ContextMenuManager cmMgr = ContextMenuService.GetManager(element);
      if(cmMgr==null)
      {
        cmMgr =
    new ContextMenuManager();
        cmMgr.OpenMode =
    OpenMode.None;
       
    ContextMenuService.SetManager(element, cmMgr);
        cmMgr.ContextMenu =
    new XamContextMenu();
        cmMgr.ContextMenu.Placement =
    PlacementMode.Center;
      }
      
      cmMgr.ContextMenu.ItemsSource = element.Text;
      cmMgr.ContextMenu.IsOpen =
    true;
    }

    Regards,
    Marin

Reply Children