2013-12-21 91 views
0

我仍然試圖做到這一點。不知道如何。我有一個列表框拖動&拖放移動項目並添加新的。我想要做的是當用戶用鼠標左鍵點擊項目什麼都沒有發生。當他用鼠標右鍵點擊時,它會選擇它(多選)。我試過e.Handled = true,問題是它不允許用戶用鼠標滾動。我的列表框活動:只允許通過在ListBox中單擊鼠標右鍵來選擇項目

private void LB_SongList_Drop(object sender, DragEventArgs e) 
    { 
     ListBox parent = sender as ListBox; 
     Song data = e.Data.GetData(typeof(Song)) as Song; 
     Song objectToPlaceBefore = GetObjectDataFromPoint(parent, e.GetPosition(parent)) as Song; 
     if (data != null && objectToPlaceBefore != null) 
     { 
      [...]//Code that moves object 

      parent.SelectedItems.Remove(data); 
     } 
     else 
     { 
      String[] file = e.Data.GetData(DataFormats.FileDrop, true) as String[]; 
      if (file != null) 
      { 
       [...]//Code that adds new data 
      } 
     } 
    } 
    private void LB_SongList_PreviewMouseMove(object sender, MouseEventArgs e) 
    { 
     if (!b_IsScrolling) 
     { 
      if (e.LeftButton == MouseButtonState.Pressed) 
      { 
       MainPointer.Main_AllowClose = false; 
       ListBox parent = sender as ListBox; 
       Song data = GetObjectDataFromPoint(parent, e.GetPosition(parent)) as Song; 
       if (data != null) 
       { 
        parent.SelectedItems.Remove(data); 
        DragDrop.DoDragDrop(parent, data, DragDropEffects.Move); 
       } 
      } 
     } 
    } 
    private static object GetObjectDataFromPoint(ListBox source, Point point) 
    { 
     UIElement element = source.InputHitTest(point) as UIElement; 
     if (element != null) 
     { 
      object data = DependencyProperty.UnsetValue; 
      while (data == DependencyProperty.UnsetValue) 
      { 
       data = source.ItemContainerGenerator.ItemFromContainer(element); 
       if (data == DependencyProperty.UnsetValue) element = VisualTreeHelper.GetParent(element) as UIElement; 
       if (element == source) return null; 
      } 
      if (data != DependencyProperty.UnsetValue) return data; 
     } 
     return null; 
    } 
    private void LB_SongList_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e) 
    { 
     if (e.LeftButton == MouseButtonState.Pressed) 
     { 
      ListBox parent = sender as ListBox; 
      Song data = GetObjectDataFromPoint(parent, e.GetPosition(parent)) as Song; 
      if (data != null) 
      { 
       LB_SongList.SelectedItems.Remove(data); 
       [...]//Code that plays a song on double click (should allow only left mouse button) 
      } 
     } 
    } 
    private void LB_SongList_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 

     if (LB_SongList.SelectedItems.Count != 0) 
     { 
      [...] 
     } 
     else 
     { 
      [...] 
     } 
    } 

任何人都可以幫忙嗎?鼠標左鍵不選擇任何項目。鼠標右鍵可以選擇項目(多個)。

+0

請將您的xaml發送給我們。 –

回答

0

再次看看你的代碼。在你的如果你正在問e.LeftButton,只需將其更改爲e.RightButoon ...

相關問題