In WPF, once the right-click menu is set, it is difficult to control it not to open it. Right-click will certainly pop up. If only a few items are hidden, it is okay, but if all items are hidden, the right-click menu of blank content is displayed, Which is ugly and does not provide any background control methods. Therefore, a foreground attribute can be controlled.
< Window X: Class = "Wpftest. window1" Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml" Title = "Window1" Height = "300" Width = "300" > < Grid > < Grid. contextmenu > < Contextmenu Contextmenuservice. isenabled =" {Binding iscontextmenuvisible} " > < Menuitem Header = "Test" /> </ Contextmenu > </ Grid. contextmenu > </ Grid > </ Window >
Namespace Wpftest { Public Partial Class Window1: window { Public Static Readonly Dependencyproperty iscontextmenuvisibleproperty = Dependencyproperty. Register ( " Iscontextmenuvisible " , Typeof (Boolean ),Typeof (Window1 ), New Propertymetadata ()); Public Window1 () {initializecomponent (); datacontext = This ;}}}
Because the default value is false, the right-click menu is not displayed by default. when it needs to be displayed, insertCode
Setvalue (iscontextmenuvisibleproperty,True);
The principle is to change the Dependency Property to control whether the right-click menu can be used. Unfortunately, this is a static additional property and cannot be accessed in the background, anyone who knows how to access it in the background can tell them that the binding method is currently only feasible.