<p>Apparantly when users right-click in our WPF application, and they use the Windows Classic theme, the default ContextMenu of the TextBox (which contains Copy, Cut and Paste) has a black background.</p>

<p>I know this works well:</p>

<pre><code>&lt;Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;

  &lt;TextBox ContextMenu="{x:Null}"/&gt;

&lt;/Page&gt;
</code></pre>

<p>But this doesn't work:</p>

<pre><code>&lt;Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;

&lt;Page.Resources&gt;

 &lt;Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}"&gt;
   &lt;Setter Property="ContextMenu" Value="{x:Null}"/&gt;
&lt;/Style&gt;
&lt;/Page.Resources&gt;

  &lt;TextBox/&gt;
&lt;/Page&gt; 
</code></pre>

<p>Does anyone know how to style or disable the default ContextMenu for all TextBoxes in WPF?</p>

[{'answer_id': 9864, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>Try removing the x:Key attribute from the Style resource, leaving TargetType.  I know, you\'re supposed to have that x:Key for a resource, but if you have it along with your TargetType the Key prevails. </p>\n\n<p>Here\'s a sample style that I use in a project to skin all tooltips in one of my apps (this is in App.Resources--notice, no Key) </p>\n\n<pre><code> &lt;Style\n    TargetType="{x:Type ToolTip}"&gt;\n    &lt;Setter\n      Property="Template"&gt;\n      &lt;Setter.Value&gt;\n        &lt;ControlTemplate\n          TargetType="{x:Type ToolTip}"&gt;\n          &lt;Grid\n            Width="{TemplateBinding Width}"\n            Height="{TemplateBinding Height}"&gt;\n            &lt;Rectangle\n              RadiusX="9"\n              RadiusY="9"\n              Stroke="LightGray"\n              StrokeThickness="2"&gt;\n              &lt;Rectangle.Fill&gt;\n                &lt;RadialGradientBrush&gt;\n                  &lt;GradientStop /&gt;\n                  &lt;GradientStop\n                    Color="FloralWhite"\n                    Offset="0" /&gt;\n                  &lt;GradientStop\n                    Color="Cornsilk"\n                    Offset="2" /&gt;\n                &lt;/RadialGradientBrush&gt;\n              &lt;/Rectangle.Fill&gt;\n            &lt;/Rectangle&gt;\n            &lt;ContentPresenter\n              Margin="6 4 6 4" /&gt;\n          &lt;/Grid&gt;\n        &lt;/ControlTemplate&gt;\n      &lt;/Setter.Value&gt;\n    &lt;/Setter&gt;\n  &lt;/Style&gt;\n</code></pre>\n'}, {'answer_id': 9878, 'author': 'Arcturus', 'author_id': 900, 'author_profile': 'https://Stackoverflow.com/users/900', 'pm_score': 2, 'selected': False, 'text': '<p>Doesn\'t matter, if you do not provide a key, it will use the <code>TargetType</code> as key just the same way my example uses :)</p>\n\n<p>Taken from MSDN on Style:</p>\n\n<blockquote>\n  <p>Setting the <code>TargetType</code> property to the <code>TextBlock</code> type without\n  setting an <code>x:Key</code> implicitly sets the <code>x:Key</code> to <code>{x:Type TextBlock}</code>. This also means that if you > > give the above Style an <code>x:Key</code> value of anything other than <code>{x:Type TextBlock}</code>, the Style\n  would not be applied to all TextBlock elements automatically. Instead,\n  you need to apply the style to the <code>TextBlock</code> elements explicitly.</p>\n</blockquote>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.windows.style.targettype.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.windows.style.targettype.aspx</a></p>\n'}, {'answer_id': 9897, 'author': 'Brad Leach', 'author_id': 708, 'author_profile': 'https://Stackoverflow.com/users/708', 'pm_score': 5, 'selected': False, 'text': '<p>To style ContextMenu\'s for all TextBoxes, I would do something like the following:</p>\n\n<p>First, in the resources section, add a ContextMenu which you plan to use as your standard ContextMenu in a textbox.<br /> e.g.</p>\n\n<pre><code>&lt;ContextMenu x:Key="TextBoxContextMenu" Background="White"&gt;\n  &lt;MenuItem Command="ApplicationCommands.Copy" /&gt;\n  &lt;MenuItem Command="ApplicationCommands.Cut" /&gt;\n  &lt;MenuItem Command="ApplicationCommands.Paste" /&gt;\n&lt;/ContextMenu&gt;\n</code></pre>\n\n<p>Secondly, create a style for your TextBoxes, which uses the context menu resource:</p>\n\n<pre><code>&lt;Style TargetType="{x:Type TextBox}"&gt;\n  &lt;Setter Property="ContextMenu" Value="{StaticResource TextBoxContextMenu}" /&gt;\n&lt;/Style&gt;\n</code></pre>\n\n<p>Finally, use your text box as normal:</p>\n\n<pre><code>&lt;TextBox /&gt;\n</code></pre>\n\n<p>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:</p>\n\n<pre><code>&lt;TextBox ContextMenu="{StaticResource TextBoxContextMenu}" /&gt;\n</code></pre>\n\n<p>Hope this helps!</p>\n'}, {'answer_id': 2523372, 'author': 'Arcturus', 'author_id': 900, 'author_profile': 'https://Stackoverflow.com/users/900', 'pm_score': 4, 'selected': True, 'text': '<p>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.</p>\n\n<p>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.</p>\n\n<p>We decided to "remove" the menu based on Brads answer, like so:</p>\n\n<pre><code>&lt;ContextMenu x:Key="TextBoxContextMenu" Width="0" Height="0" /&gt;\n</code></pre>\n\n<p>And use this empty context menu like so:</p>\n\n<pre><code>&lt;Style TargetType="{x:Type TextBox}"&gt;\n  &lt;Setter Property="ContextMenu" Value="{StaticResource TextBoxContextMenu}" /&gt;\n&lt;/Style&gt;\n</code></pre>\n'}, {'answer_id': 3484929, 'author': 'Robin Davies', 'author_id': 415713, 'author_profile': 'https://Stackoverflow.com/users/415713', 'pm_score': 5, 'selected': False, 'text': '<p>Bizarre. <code>ContextMenu="{x:Null}"</code> doesn\'t do the trick. </p>\n\n<p>This does, however: </p>\n\n<pre><code>&lt;TextBox.ContextMenu&gt;\n    &lt;ContextMenu Visibility="Collapsed"&gt;\n    &lt;/ContextMenu&gt;\n&lt;/TextBox.ContextMenu&gt;\n</code></pre>\n'}, {'answer_id': 53862931, 'author': 'MrBi', 'author_id': 10765214, 'author_profile': 'https://Stackoverflow.com/users/10765214', 'pm_score': 2, 'selected': False, 'text': '<p>This is way is what I always use:</p>\n<pre><code>      &lt;TextBox x:Name=&quot;MyTextbox&quot;&gt;\n         &lt;TextBox.ContextMenu&gt;\n         &lt;ContextMenu Visibility=&quot;Hidden&quot;/&gt;\n         &lt;/TextBox.ContextMenu&gt;\n      &lt;/TextBox&gt;\n</code></pre>\n<p>And also can use:</p>\n<pre class="lang-cs prettyprint-override"><code>           MyTextbox.ContextMenu.Visibility = Visibility.Hidden;\n           MyTextbox.ContextMenu.Visibility = Visibility.Visble;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/9632', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/900/']