Saturday 15 February 2014

c# - Placing a Context Menu relative to button -


I'm trying to do the same thing except what I mentioned, except the rectangle. What I have described in the answer (among many other things), and I have shown it in relation to the rectangular (which is being used by a button in my case).

I am using a rectangle as a button because I did not like the mouseover effect of a button and it was easy to use just one rectangle instead of creating a custom button. As a note, I also had this problem when I was using a button, in Expression Mix 4.

This is XAML for my rectangle:

  & lt; Rectangle X: name = "rectangle" horizontal alignment = "left" mouseDown = "menuclick" height = "55" vertical alignment = "top" width = "135" grid. Colspan = "2" margin = "0,1,0,0" ContextMenuService.Placement = "down" & gt; & Lt; Rectangle.ContextMenu & gt; & Lt; ContextMenu x: name = "menu" & gt; & Lt; MenuItem x: name = "load xml" header = "load xml" command = "{binding command}" commandpalmter = "browse xml" /> & Lt; MenuItem x: name = "savexml" header = "save xml" command = "{binding command}" command parameter = "save xml" /> & Lt; MenuItem x: name = "savebary" header = "save binary" command = "{binding command}" commandpalmator = "save" /> & Lt; MenuItem x: Name = "SaveText" header = "save text" command = "{binding command}" commandpalmator = "savest" /> & Lt; / ContextMenu & gt; & Lt; /Rectangle.ContextMenu> & Lt; Rectangle.Fill & gt; & Lt; ImageBrush ImageSource = "Resource / Logo .png" Stretch = "Uniform" /> & Lt; /Rectangle.Fill> & Lt; / Rectangles & gt;   

It still displays the context menu directly next to the mouse click point if I set the placement below within the context menu code, then add context menu service bits for reference Instead, it displays at the bottom or top relative to the entire window.

Any help would be appreciated. Thanks!

EDIT: I am also using a method to enable left-click of the rectangle, on mouseDown, it calls this method:

  Private Zero Menuclaim (Object Sender, System. Routed Event ARG E) {Menu.PlacementTarget = this; Menu.IsOpen = true; }    

You must set the ContextMenuService.PlacementTarget property So the framework knows which element should be related to the context menu, for example:

     

Just add that property to your rectangle declaration. When mixed with ContextMenuService.Placement = "Bottom" , the context menu will show as below as you wanted.

Edit: Because you're opening the programmaticly, you can directly click on the Placement and PlacementTarget properties on the menu Has to be set. Attribute attributed to ContextMenuService , when set to owner of the menu, will only be effective if the context menu is opened by traditional means, for example, right click your mouse in the handler, you can PlacementTarget = this; are set, but you are PlacementTarget = rectangle; . should be set.

No comments:

Post a Comment