Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Weird behavior with contextual menu not rendering properly.

Weird behavior with contextual menu not rendering properly.

Scheduled Pinned Locked Moved WPF
wpfdevopsquestionlearning
3 Posts 2 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    I have a button with a contextual menu attached to it, I want display the menu on the left click instead of the default right click. I'm using a company wide "resource pack" that defines some style. When I left click on the button to open the menu, the menu is drawn/rendered using the default windows style and the event/commands are not triggered. When I right click on the button to open the menu, the menu is drawn/rendered using our styles and events/commands are working. When I left click again the menu is drawn/rendered using our styles and event/commands are working I'm not sure why the wrong menu is loaded once. Any insights or things I might have missed ?

        private void ButtonBase\_OnClick(object sender, RoutedEventArgs e)
        {
            if (MyContextMenu.ContextMenu != null) 
                MyContextMenu.ContextMenu.IsOpen = true;
        }
    

    CI/CD = Continuous Impediment/Continuous Despair

    Richard DeemingR 1 Reply Last reply
    0
    • M Maximilien

      I have a button with a contextual menu attached to it, I want display the menu on the left click instead of the default right click. I'm using a company wide "resource pack" that defines some style. When I left click on the button to open the menu, the menu is drawn/rendered using the default windows style and the event/commands are not triggered. When I right click on the button to open the menu, the menu is drawn/rendered using our styles and events/commands are working. When I left click again the menu is drawn/rendered using our styles and event/commands are working I'm not sure why the wrong menu is loaded once. Any insights or things I might have missed ?

          private void ButtonBase\_OnClick(object sender, RoutedEventArgs e)
          {
              if (MyContextMenu.ContextMenu != null) 
                  MyContextMenu.ContextMenu.IsOpen = true;
          }
      

      CI/CD = Continuous Impediment/Continuous Despair

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      The WPF ContextMenu doesn't inherit the DataContext of its parent element. You need to set that explicitly for the bindings to work:

      <ContextMenu x:Name="MyContextMenu" DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">

      As for the styles not being applied when you left-click, have you tried different sequences? Try two left-clicks, or a right-click followed by a left click, to see whether the styles are applied the second time the menu appears, or only applied after a right-click.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        The WPF ContextMenu doesn't inherit the DataContext of its parent element. You need to set that explicitly for the bindings to work:

        <ContextMenu x:Name="MyContextMenu" DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">

        As for the styles not being applied when you left-click, have you tried different sequences? Try two left-clicks, or a right-click followed by a left click, to see whether the styles are applied the second time the menu appears, or only applied after a right-click.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        Adding the datacontext does not change anything. The menu loads correctly the first when I right click on the button; after that it loads correctly on the left click. It's not urgent for now, I'm prototyping a UI. , but slightly annoying. Thanks.

        CI/CD = Continuous Impediment/Continuous Despair

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups