[ACCEPTED]-WPF: How to style or disable the default ContextMenu of a TextBox-.net-3.5
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.
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 7 for your TextBoxes, which uses the context 6 menu resource:
<Style TargetType="{x:Type TextBox}">
<Setter Property="ContextMenu" Value="{StaticResource TextBoxContextMenu}" />
</Style>
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!
Bizarre. ContextMenu="{x:Null}"
doesn't do the trick.
This does, however:
<TextBox.ContextMenu>
<ContextMenu Visibility="Collapsed">
</ContextMenu>
</TextBox.ContextMenu>
0
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}" />
</Style>
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 theTextBlock
type 7 without setting anx:Key
implicitly sets the 6x:Key
to{x:Type TextBlock}
. This also means that if you > > give 5 the above Style anx: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 theTextBlock
elements 1 explicitly.
http://msdn.microsoft.com/en-us/library/system.windows.style.targettype.aspx
This is way is what I always use:
<TextBox x:Name="MyTextbox">
<TextBox.ContextMenu>
<ContextMenu Visibility="Hidden"/>
</TextBox.ContextMenu>
</TextBox>
And also 1 can use:
MyTextbox.ContextMenu.Visibility = Visibility.Hidden;
MyTextbox.ContextMenu.Visibility = Visibility.Visble;
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)
<Style
TargetType="{x:Type ToolTip}">
<Setter
Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="{x:Type ToolTip}">
<Grid
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}">
<Rectangle
RadiusX="9"
RadiusY="9"
Stroke="LightGray"
StrokeThickness="2">
<Rectangle.Fill>
<RadialGradientBrush>
<GradientStop />
<GradientStop
Color="FloralWhite"
Offset="0" />
<GradientStop
Color="Cornsilk"
Offset="2" />
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter
Margin="6 4 6 4" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.