【问题标题】:how to disable cut, copy paste and right click across all WPF forms如何在所有 WPF 表单中禁用剪切、复制粘贴和右键单击
【发布时间】:2017-11-01 08:02:14
【问题描述】:

我们正在构建一个 WPF 信息亭应用程序。 我们需要禁用 CUT COPY PASTE 和 RIGHT-CLICKs

请问这是怎么做到的?

这篇 SO 帖子并未为所有表单提供集中式解决方案:

How to suppress Cut, Copy and Paste Operations in TextBox in WPF?

【问题讨论】:

  • 只是那些你想禁用的东西还是所有的 ContextMenu 项?顺便说一句,WPF 中没有表格,请使用 WindowsControls

标签: wpf keyboard-shortcuts


【解决方案1】:

您需要在您定义的App.xaml 中添加Style

<Style TargetType="TextBox"> 
<!-- OR -->
<Style TargetType="{x:Type TextBox}">
    <Setter Property="ContextMenu" Value="{x:Null}"/>
</Style>  

但这仅适用于不在DataTemplate 中的项目。
更新:
App.xaml

<Application x:Class="TestApp.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:TestApp"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="ContextMenu" Value="{x:Null}"/>
    </Style>
</Application.Resources>


这是MainWindow.xaml

<Window x:Class="TestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xml:lang="en-GB"
    xmlns:local="clr-namespace:TestApp"
    xmlns:converter="clr-namespace:TestApp.Converters"
    mc:Ignorable="d"
    Height="478.889" Width="903.889">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0.3*"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <StackPanel>
        <TextBox Name="txtBx" MinHeight="150" 
                 VerticalAlignment="Top" AutoWordSelection="True" 
                 MaxLines="10" 
                 TextWrapping="WrapWithOverflow" 
                 SelectionChanged="txtBx_TextHighlighted"
                 ToolTip="{x:Null}"
                 Margin="10"/>

    </StackPanel>
</Grid>


如果您右键单击 TextBox,您将无法使用任何 ContextMenu
更新 2:
继续我们的聊天,TextBox 引用了其他样式,这些样式覆盖了我们在App.xaml 中设置的任何内容。由于外部样式是在App.xaml 之后加载的。

【讨论】:

  • 那么您的TextBoxes 位于DataTemplates 中,或者您的应用不在WPF 中而位于UWP 中? @CharlesOkwuagwu
  • 它是 wpf,不,我不使用 DataTemplates
  • @CharlesOkwuagwu 你能告诉我们你用的是什么吗?如果您可以在此处放置示例代码,说明您如何使用这些TextBoxes,那么我会更容易为您提供更多帮助。
  • 我在一个标准窗口对象上使用它们,在一个网格中
  • 好的,我将举一个例子来说明我是如何在我的应用程序中使用样式的,你可以告诉我它是否与你的不同。
猜你喜欢
  • 1970-01-01
  • 2015-08-08
  • 2014-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-17
相关资源
最近更新 更多