WPF:如何设置或禁用 TextBox 的默认 ContextMenu
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF: How to style or disable the default ContextMenu of a TextBox
提问by Arcturus
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.
显然,当用户在我们的 WPF 应用程序中右键单击并使用 Windows 经典主题时,TextBox(包含复制、剪切和粘贴)的默认 ContextMenu 具有黑色背景。
I know this works well:
我知道这很有效:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextBox ContextMenu="{x:Null}"/>
</Page>
But this doesn't work:
但这不起作用:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">
<Setter Property="ContextMenu" Value="{x:Null}"/>
</Style>
</Page.Resources>
<TextBox/>
</Page>
Does anyone know how to style or disable the default ContextMenu for all TextBoxes in WPF?
有谁知道如何为 WPF 中的所有文本框设置样式或禁用默认的 ContextMenu?
采纳答案by Arcturus
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.
由于迟来的错误报告,我们发现我们无法直接在部分受信任的应用程序中使用 ApplicationComands Cut Paste 和 Copy。因此,在您的控件的任何命令中使用这些命令在执行时绝对不会做任何事情。
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.
所以本质上 Brads 的答案几乎就在那里,它看起来确实是正确的方式,即没有黑色背景,但没有解决问题。
We decided to "remove" the menu based on Brads answer, like so:
我们决定根据 Brads 的回答“删除”菜单,如下所示:
<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>
回答by Brad Leach
To style ContextMenu's for all TextBoxes, I would do something like the following:
要为所有文本框设置 ContextMenu 的样式,我会执行以下操作:
First, in the resources section, add a ContextMenu which you plan to use as your standard ContextMenu in a textbox.
e.g.
首先,在资源部分,添加一个 ContextMenu,您计划将其用作文本框中的标准 ContextMenu。
例如
<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!
希望这可以帮助!
回答by Robin Davies
Bizarre. ContextMenu="{x:Null}"
doesn't do the trick.
奇怪。ContextMenu="{x:Null}"
没有用。
This does, however:
然而,这确实:
<TextBox.ContextMenu>
<ContextMenu Visibility="Collapsed">
</ContextMenu>
</TextBox.ContextMenu>
回答by Arcturus
Doesn't matter, if you do not provide a key, it will use the TargetType
as key just the same way my example uses :)
没关系,如果您不提供密钥,它将TargetType
像我的示例使用的那样使用as 密钥:)
Taken from MSDN on Style:
取自 MSDN 上的风格:
Setting the
TargetType
property to theTextBlock
type without setting anx:Key
implicitly sets thex:Key
to{x:Type TextBlock}
. This also means that if you > > give the above Style anx:Key
value of anything other than{x:Type TextBlock}
, the Style would not be applied to all TextBlock elements automatically. Instead, you need to apply the style to theTextBlock
elements explicitly.
将
TargetType
属性设置为TextBlock
类型而不设置 anx:Key
隐式设置x:Key
为{x:Type TextBlock}
。这也意味着,如果您 >> > 给上述 Style 一个x:Key
除 之外的任何值{x:Type TextBlock}
,则该 Style 不会自动应用于所有 TextBlock 元素。相反,您需要TextBlock
显式地将样式应用于元素。
http://msdn.microsoft.com/en-us/library/system.windows.style.targettype.aspx
http://msdn.microsoft.com/en-us/library/system.windows.style.targettype.aspx
回答by MrBi
This is way is what I always use:
这是我一直使用的方式:
<TextBox x:Name="MyTextbox">
<TextBox.ContextMenu>
<ContextMenu Visibility="Hidden"/>
</TextBox.ContextMenu>
</TextBox>
And also can use:
并且还可以使用:
MyTextbox.ContextMenu.Visibility = Visibility.Hidden;
MyTextbox.ContextMenu.Visibility = Visibility.Visble;
回答by MrBi
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.
尝试从 Style 资源中删除 x:Key 属性,保留 TargetType。我知道,您应该拥有资源的 x:Key,但如果您拥有它和 TargetType,则 Key 将占上风。
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)
这是我在一个项目中使用的示例样式,用于为我的一个应用程序中的所有工具提示设置外观(这是在 App.Resources 中——注意,没有键)
<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>