1

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

所以我希望当我执行 MouseLeftDown 并移动列表框项时,请拖动。当 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);
    }
4

2 回答 2

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;
        }
    }
于 2013-02-26T14:43:22.840 回答
0

将链接到 UserControl 的事件更改为 mouseup 而不是单击。这样,只有完全单击才会触发更改用户控件的代码,并且拖动将不受阻碍地进行。是关于 MouseUp 事件的 MSDN 链接:)

于 2013-02-25T16:29:53.270 回答