简体   繁体   English

在D&D中禁用列表框CTRL + C和CTRL + X

[英]Disable list box CTRL+C & CTRL+X in D&D

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. 我需要禁用在列表框中使用CTRL + CCTRL + X的功能。 I don't want it to be fired by the keyboard. 我不希望它被键盘触发。 Is there an option to prevent it in WPF? 在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);
                }
            }
        }
    }

}

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 : 一种方法是处理UIElement.PreviewKeyDown事件 ,检测相关的键,然后将e.Handled属性设置为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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM