禁用列表框 CTRL+C &D&D 中的 CTRL+X [英] Disable list box CTRL+C & CTRL+X in D&D

查看:32
本文介绍了禁用列表框 CTRL+C &D&D 中的 CTRL+X的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有拖放选项到文本框的列表视图.我需要禁用在列表框中使用 CTRL+CCTRL +X 的能力.我不希望它被键盘触发.在 WPF 中是否有阻止它的选项?

I have a list view with drag and drop option to text box. I need to disable the ability to use CTRL+C or CTRL +X on the list box. I don't want it to be fired by the keyboard. Is there an option to prevent it in WPF?

  <ListBox  x:Name="Lst" IsSelected="False"  Height="115" Width="150" ItemsSource="{Binding UsCollectionView}" 
             SelectionChanged="listbox_SelectionChanged" AllowDrop="True" PreviewDrop="ListBox_PreviewDrop" 
    </ListBox>


private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (sender is ListBox)
    {
        var listBox = sender as ListBox;
        if (e.AddedItems.Count == 1)
        {
            if (listBox.SelectedItem != null)
            {
                var mySelectedItem = listBox.SelectedItem as User;
                if (mySelectedItem != null)
                {
                    DragDrop.DoDragDrop(listBox, mySelectedItem.Name, DragDropEffects.Copy | DragDropEffects.Move);
                }
            }
        }
    }

}

推荐答案

有很多方法可以做到这一点.一种方法是处理 UIElement.PreviewKeyDown 事件,检测相关键,然后设置e.Handled属性为true:

There are a number of way that you can do that. One way is to handle the UIElement.PreviewKeyDown Event, detect the relevant keys and then set the e.Handled property to true:

private void ListBoxPreviewKeyDown(object sender, KeyEventArgs e)
{
    if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
    {
        // Ctrl Key is pressed
        if (e.Key == Key.C || e.Key == Key.X) e.Handled = true;
    }
}

这篇关于禁用列表框 CTRL+C &amp;D&D 中的 CTRL+X的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆