WPF: How to style or disable the default ContextMenu of a TextBox WPF: How to style or disable the default ContextMenu of a TextBox wpf wpf

WPF: How to style or disable the default ContextMenu of a TextBox


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

First, in the resources section, add a ContextMenu which you plan to use as your standard ContextMenu in a textbox.
e.g.

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

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

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

Finally, use your text box as normal:

<TextBox />

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

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

Hope this helps!


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

This does, however:

<TextBox.ContextMenu>    <ContextMenu Visibility="Collapsed">    </ContextMenu></TextBox.ContextMenu>


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

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

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

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

And use this empty context menu like so:

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