Reputation: 696
I have a Listbox a MainControl and 4 PreviewControls. I would like when i click an item from the listbox that the mainControl changes (This happens). But i would like to add a dragg behavior to the listboxitems so i can drag a listboxitem to one of the previewControls. I tried this but when i try to drag the listboxitem the selectionchanged event is always fired.
So i would like that when i do MouseLeftDown and move the listboxitem, Do drag. And When Click or MouseLefUp (selectionchanged) occurs the MainControl changes.
I tried this but PreviewMouseLeftButtonDown event again the other events aren't executed.
private void lstCameras_PreviewMouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
{
startPoint = e.GetPosition(null);
if (!Test) e.Handled = true;
else
{
Test = false;
}
Console.WriteLine("lstCameras_PreviewMouseLeftButtonDown_1 Handled = " + e.Handled);
}
private void lstCameras_PreviewMouseUp_1(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("lstCameras_MouseUp_1");
if (!Test)
{
Test = true;
lstCameras.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, Environment.TickCount, MouseButton.Left) { RoutedEvent = ListBox.PreviewMouseLeftButtonDownEvent });
}
else
{
Test = false;
}
Console.WriteLine("end lstCameras_MouseUp_1 Test = " + Test);
}
Upvotes: 1
Views: 540
Reputation: 696
Don't know if it is the best way but it works.
private object GetDataFromListBox(ListBox source, Point point)
{
DependencyObject element = source.InputHitTest(point) as DependencyObject;
if (element != null)
{
object data = DependencyProperty.UnsetValue;
while (data == DependencyProperty.UnsetValue)
{
data = source.ItemContainerGenerator.ItemFromContainer(element);
if (data == DependencyProperty.UnsetValue)
{
element = (DependencyObject)VisualTreeHelper.GetParent(element);
}
if (element == source)
{
return null;
}
}
if (data != DependencyProperty.UnsetValue)
{
return data;
}
}
return null;
}
private void lstCameras_PreviewMouseMove_1(object sender, MouseEventArgs e)
{
Point mousePos = e.GetPosition(null);
Vector diff = startPoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed && this.DataContext != null &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
ListBox parent = (ListBox)sender;
Item data = (Item)GetDataFromListBox(parent, e.GetPosition(parent));
if (data != null)
{
DataObject dragData = new DataObject(typeof(Item), data);
DragDrop.DoDragDrop(this, dragData, DragDropEffects.All);
}
}
}
private void lstCameras_PreviewMouseDown_1(object sender, MouseButtonEventArgs e)
{
ListBox parent = (ListBox)sender;
Item data = (Item)GetDataFromListBox(parent, e.GetPosition(parent));
if (data != null )
{
startPoint = e.GetPosition(null);
e.Handled = true;
}
}
private void lstCameras_PreviewMouseUp_1(object sender, MouseButtonEventArgs e)
{
ListBox parent = (ListBox)sender;
Item data = (Item)GetDataFromListBox(parent, e.GetPosition(parent));
if (data != null)
{
lstCameras.SelectedItem = data;
}
}
Upvotes: 1
Reputation: 27336
Change the event linked to UserControl to mouseup instead of click. That way only a full click will fire the code that changes the user control, and the drag will proceed unhindered. Here is a link to MSDN on MouseUp events :)
Upvotes: 0