0

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

}
DiligentKarma
  • 5,198
  • 1
  • 30
  • 33
mileyH
  • 176
  • 1
  • 5
  • 20

1 Answers1

0

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;
    }
}
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Since i new to this topic should I bound it to the textBOx xaml?how should this event fired? Thanks a lot! – mileyH Jan 13 '14 at 13:48
  • You said that you wanted to stop users from pressing those keys *on the list box*, so I would attach this handler... to the `ListBox`. You can do that either by XAML, or by code, it's up to you. – Sheridan Jan 13 '14 at 13:56