Hide the context menu in WPF

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.