Disable list box CTRL+C & CTRL+X in D&D

2019-06-14 02:28发布

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);
                }
            }
        }
    }

}

标签: c# wpf xaml mvvm
1条回答
戒情不戒烟
2楼-- · 2019-06-14 02:44

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;
    }
}
查看更多
登录 后发表回答