首页 / 知识

关于xaml:WPF:如何设置或禁用文本框的默认ContextMenu

2023-04-12 02:58:00

关于xaml:WPF:如何设置或禁用文本框的默认ContextMenu

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

显然,当用户在我们的WPF应用程序中单击鼠标右键,并且使用Windows Classic主题时,TextBox的默认ContextMenu(包含Copy,Cut和Paste)具有黑色背景。

我知道这很好用:

1
2
3
4
5
6
7
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <TextBox ContextMenu="{x:Null}"/>

</Page>

但这不起作用:

1
2
3
4
5
6
7
8
9
10
11
12
<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>

有谁知道如何为WPF中的所有TextBox设置样式或禁用默认的ContextMenu?


要为所有TextBox设置ContextMenu的样式,我将执行以下操作:

首先,在资源部分中,添加一个ContextMenu,您打算将其用作文本框中的标准ContextMenu。
例如

1
2
3
4
5
<ContextMenu x:Key="TextBoxContextMenu" Background="White">
  <MenuItem Command="ApplicationCommands.Copy" />
  <MenuItem Command="ApplicationCommands.Cut" />
  <MenuItem Command="ApplicationCommands.Paste" />
</ContextMenu>

其次,为您的TextBoxes创建一种样式,该样式使用上下文菜单资源:

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

最后,照常使用文本框:

1
<TextBox />

相反,如果您只想将此上下文菜单仅应用于某些文本框,则不要创建上面的样式,而是将以下内容添加到TextBox标记中:

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

希望这可以帮助!


奇怪ContextMenu="{x:Null}"不能解决问题。

但是,这样做:

1
2
3
4
<TextBox.ContextMenu>
    <ContextMenu Visibility="Collapsed">
    </ContextMenu>
</TextBox.ContextMenu>

由于最新的错误报告,我们发现我们不能在部分受信任的应用程序中直接使用ApplicationComands剪切粘贴和复制。因此,在您的任何命令中使用这些命令在执行时绝对没有任何作用。

因此,从本质上讲,布拉德斯的回答几乎就在那儿,它的确看起来是正确的方式,即没有黑色背景,但没有解决问题。

我们决定根据Brads答案"删除"菜单,如下所示:

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

并使用如下所示的空上下文菜单:

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

没关系,如果您不提供密钥,它将使用TargetType作为密钥,就像我的示例使用的一样:)

来自MSDN的Style:

Setting the TargetType property to the TextBlock type without
setting an x:Key implicitly sets the x:Key to {x:Type TextBlock}. This also means that if you > > give the above Style an x: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 the TextBlock elements explicitly.

http://msdn.microsoft.com/zh-CN/library/system.windows.style.targettype.aspx


这就是我一直使用的方式:

1
2
3
4
5
      <TextBox x:Name="MyTextbox">
         <TextBox.ContextMenu>
         <ContextMenu Visibility="Hidden"/>
         </TextBox.ContextMenu>
      </TextBox>

也可以使用:

1
2
           MyTextbox.ContextMenu.Visibility = Visibility.Hidden;
           MyTextbox.ContextMenu.Visibility = Visibility.Visble;

尝试从Style资源中删除x:Key属性,并保留TargetType。我知道,您应该为资源使用x:Key,但如果将它与TargetType一起使用,则以Key为准。

这是我在项目中使用的示例样式,用于对我的一个应用程序中的所有工具提示进行外观设置(这在App.Resources中-注意,无键)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 <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>


默认文本框用户应用程序

最新内容

猜你喜欢