I'm still trying to do this. No clue how. I have a ListBox with Drag&Drop to move items and add new. What I'm trying to do is when user clicks on item with left mouse button nothing happens. And when he clicks with right mouse button it selects it (Multiple selection).
I have tried e.Handled = true
, the problem is that it doesn't allow user to scroll with mouse. My listbox's events:
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
{
[...]
}
}
Anyone could help with this? Left Mouse button doesn't select any item. Right Mouse Button does select items (Multiple).