[ACCEPTED]-WPF: How to style or disable the default ContextMenu of a TextBox-.net-3.5

Accepted answer
Score: 32

To style ContextMenu's for all TextBoxes, I 11 would do something like the following:

First, in 10 the resources section, add a ContextMenu 9 which you plan to use as your standard ContextMenu 8 in a textbox.

<ContextMenu x:Key="TextBoxContextMenu" Background="White">
  <MenuItem Command="ApplicationCommands.Copy" />
  <MenuItem Command="ApplicationCommands.Cut" />
  <MenuItem Command="ApplicationCommands.Paste" />

Secondly, create a style 7 for your TextBoxes, which uses the context 6 menu resource:

<Style TargetType="{x:Type TextBox}">
  <Setter Property="ContextMenu" Value="{StaticResource TextBoxContextMenu}" />

Finally, use your text box 5 as normal:

<TextBox />

If instead you want to apply this 4 context menu to only some of your textboxes, do 3 not create the style above, and add the 2 following to your TextBox markup:

<TextBox ContextMenu="{StaticResource TextBoxContextMenu}" />

Hope this 1 helps!

Score: 22

Bizarre. ContextMenu="{x:Null}" doesn't do the trick.

This does, however:

    <ContextMenu Visibility="Collapsed">


Score: 8

Due to a late bug report we discovered that 10 we cannot use the ApplicationComands Cut 9 Paste and Copy directly in a partial trusted 8 application. Therefor, using these commands 7 in any Commmand of your controls will do 6 absolutely nothing when executed.

So in essence 5 Brads answer was almost there, it sure looked 4 the right way i.e. no black background, but 3 did not fix the problem.

We decided to "remove" the 2 menu based on Brads answer, like so:

<ContextMenu x:Key="TextBoxContextMenu" Width="0" Height="0" />

And 1 use this empty context menu like so:

<Style TargetType="{x:Type TextBox}">
  <Setter Property="ContextMenu" Value="{StaticResource TextBoxContextMenu}" />
Score: 2

Doesn't matter, if you do not provide a 10 key, it will use the TargetType as key just the same 9 way my example uses :)

Taken from MSDN on 8 Style:

Setting the TargetType property to the TextBlock type 7 without setting an x:Key implicitly sets the 6 x:Key to {x:Type TextBlock}. This also means that if you > > give 5 the above Style an x:Key value of anything other 4 than {x:Type TextBlock}, the Style would not be applied 3 to all TextBlock elements automatically. Instead, you 2 need to apply the style to the TextBlock elements 1 explicitly.


Score: 2

This is way is what I always use:

      <TextBox x:Name="MyTextbox">
         <ContextMenu Visibility="Hidden"/>

And also 1 can use:

           MyTextbox.ContextMenu.Visibility = Visibility.Hidden;
           MyTextbox.ContextMenu.Visibility = Visibility.Visble;
Score: 1

Try removing the x:Key attribute from the 7 Style resource, leaving TargetType. I know, you're 6 supposed to have that x:Key for a resource, but 5 if you have it along with your TargetType 4 the Key prevails.

Here's a sample style 3 that I use in a project to skin all tooltips 2 in one of my apps (this is in App.Resources--notice, no 1 Key)

    TargetType="{x:Type ToolTip}">
          TargetType="{x:Type ToolTip}">
            Width="{TemplateBinding Width}"
            Height="{TemplateBinding Height}">
                  <GradientStop />
                    Offset="0" />
                    Offset="2" />
              Margin="6 4 6 4" />

More Related questions