datagrid - 如何在 WPF DataGrid 中禁用 CTRL 键盘
问题描述
我有一个数据网格,我想在其中阻止用户按 CTRL 键。
<ImageBrush ImageSource="/AB17035_ZLT_Client;component/Images/bum_big.PNG"
Stretch="Uniform" Opacity="0.05" />
</Border.Background>
<DataGrid Name="PlanGrid" HorizontalAlignment="Stretch" FontSize="16" RowHeight="30" HorizontalScrollBarVisibility="Visible"
VerticalAlignment="Stretch"
AutoGenerateColumns="True"
SelectionMode="Extended"
VerticalGridLinesBrush="Transparent"
Background="Transparent" RowBackground="Transparent"
ItemsSource="{Binding PlanDataView, Mode=TwoWay}"
IsReadOnly="True" SelectionUnit="FullRow" IsSynchronizedWithCurrentItem="True" Cursor="Arrow" AllowDrop="True">
<DataGrid.InputBindings>
<KeyBinding Gesture="Ctrl" Command="ApplicationCommands.NotACommand"></KeyBinding>
</DataGrid.InputBindings>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<ei:CallMethodAction MethodName="PlanGrid_Loaded" TargetObject="{Binding}"></ei:CallMethodAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
如果用户尝试按下 CTLR 键,它不应该在 wpf 数据网格上工作。
解决方案
我用谷歌搜索并找到了答案。下面是防止按 ctrl 键的一段代码。
private void PreviewKeyDown(object sender, KeyEventArgs e)
{
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
e.Handled = true;
MessageBox.Show("CTRL Key is not allowed");
}
}
还添加此功能“PreviewKeyDown”来加载数据网格的事件,如下所示:
public void PlanGrid_Loaded(object sender, RoutedEventArgs e)
{
planDatagrid.KeyDown += new KeyEventHandler(PreviewKeyDown);
}
推荐阅读
- r - 尝试在 R 中生成相关矩阵的“未使用参数”错误消息
- python - Plotly:如何查找和注释两条线之间的交点?
- timestamp - Sectigo 时间戳服务器总是使用 SHA384 而不是 SHA1
- python - 如何从列表中创建 Pandas 数据框?
- sql - SQL 是否有能力取值、使用逻辑并产生不同的值
- android - 无法销毁活动 Android:Google Play 控制台错误
- javascript - 标题栏图片显示在一页上,但不显示其他页面
- azure-devops - 显示用户输入的选择列表
- elixir - 控制器和插件 Elixir Phoenix 有什么区别?
- flutter - 将 Future 转换为字符串颤动