2014-01-13 42 views
0

我有一個列表視圖,拖放選項爲文本框。 我需要禁用使用CTRL +ç CTRL +X列表框的能力。 我不希望它被鍵盤擊中。有沒有在WPF中阻止它的選項?在D&D中禁用列表框CTRL + C&CTRL + X

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

} 

回答

0

有很多方法可以做到這一點。一種方法是處理UIElement.PreviewKeyDown Event,檢測相關按鍵,然後設置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; 
    } 
} 
+0

由於我新來這個話題我應該它綁定到文本框的XAML如何把這個觸發的事件?非常感謝! – mileyH

+0

你說你想阻止用戶按下列表框*上的那些鍵*,所以我將這個處理程序...附加到'ListBox'。您可以通過XAML或通過代碼來實現,取決於您。 – Sheridan