列表框单击并拖动

时间:2013-02-25 16:27:54

标签: c# .net wpf drag-and-drop listbox

我有一个Listbox一个MainControl和4个PreviewControls。我希望当我从列表框中单击mainControl更改的项目时(这会发生)。但是我想在listboxitems中添加一个dragg行为,这样我就可以将listboxitem拖到其中一个previewControls中。我尝试了这个但是当我尝试拖动listboxitem时,总是会触发selectionchanged事件。

所以我希望当我做MouseLeftDown并移动listboxitem时,请拖动。 当Click或MouseLefUp(selectionchanged)发生时,MainControl会发生变化。

我尝试了这个但是PreviewMouseLeftButtonDown事件再次没有执行其他事件。

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

2 个答案:

答案 0 :(得分:1)

不知道这是否是最佳方式,但它有效。

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

答案 1 :(得分:0)

将链接到UserControl的事件更改为mouseup而不是单击。这样,只需完整点击即可触发更改用户控件的代码,拖动将无阻碍地进行。 Here是MouseUp事件上MSDN的链接:)