C#WPF在画布上的StackPanel内拖放按钮

时间:2016-08-26 08:26:21

标签: c# wpf canvas drag-and-drop stackpanel

这可能是一个基本问题,但我刚刚开始使用WPF,我在尝试进行简单的拖放操作时遇到了麻烦。

我创建了这个ToolboxButton类:

public class ToolboxButton : Button
{
    private bool _isDragging = false;
    private Point _startPoint;

    public ToolboxButton(string content)
    {
        Content = content;
        HorizontalAlignment = HorizontalAlignment.Stretch;
        Height = 30;
        Loaded += ToolboxButton_Loaded;
    }

    void ToolboxButton_Loaded(object sender, RoutedEventArgs e)
    {
        PreviewMouseLeftButtonDown += ToolboxButton_PreviewMouseLeftButtonDown;
        PreviewMouseMove += ToolboxButton_PreviewMouseMove;
    }

    void ToolboxButton_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed && !_isDragging)
        {
            Point position = e.GetPosition(null);

            if (Math.Abs(position.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance ||
                    Math.Abs(position.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
            {
                StartDrag(e);
            }
        } 
    }

    void ToolboxButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        _startPoint = e.GetPosition(null);
    }

    private void StartDrag(MouseEventArgs e)
    {
        _isDragging = true;
        DataObject data = new DataObject(System.Windows.DataFormats.Text.ToString(), "abcd");
        DragDrop.DoDragDrop(e.Source as ToolboxButton, data, DragDropEffects.Move);
        _isDragging = false;
    }
}

此按钮添加到堆栈面板中,如下所示:

ToolboxButton btnAddButton = new ToolboxButton("Button");
_toolboxView.Children.Add(btnAddButton); // _toolboxView is a stackpanel

我有一个带有以下代码的Canvas:

public class DesignerView : Canvas
{
    public DesignerView()
    {
        AllowDrop = true;
        DragOver += DesignerView_DragOver;
        Drop += DesignerView_Drop;
        PreviewDragOver += DesignerView_PreviewDragOver;
    }

    void DesignerView_PreviewDragOver(object sender, DragEventArgs e)
    {
        MessageBox.Show("previewdragover");
    }

    void DesignerView_DragOver(object sender, DragEventArgs e)
    {
        MessageBox.Show("dragover");
        if (!e.Data.GetDataPresent(typeof(ToolboxButton)))
        {
            e.Effects = DragDropEffects.None;
            e.Handled = true;
        }
    }

    void DesignerView_Drop(object sender, DragEventArgs e)
    {
        MessageBox.Show("drop");
        if (e.Data.GetDataPresent(typeof(ToolboxButton)))
        {
            ToolboxButton droppedThingie = e.Data.GetData(typeof(ToolboxButton)) as ToolboxButton;
            MessageBox.Show("You dropped: " + droppedThingie.Content);
        }
    }

    public UIElement GetView()
    {
        return this;
    }
}

Canvas和StackPanel都添加在主窗口中,如下所示:

Grid contentGrid = new Grid();
Content = contentGrid;
contentGrid.Children.Add(_toolboxView.GetView());
contentGrid.Children.Add(_designerView.GetView());

所有MessageBox都没有触发,我无法找到原因。光标变为"无法固定",一个内部有对角线的黑色圆圈。 我错过了什么吗?我希望在没有XML的代码中完成所有事情。 也许我必须在StackPanel上做一些事情,但我在那里尝试了ToolboxButton的代码,它也没有用。

1 个答案:

答案 0 :(得分:0)

正如我可以看到你完成了所有工作,只需使用DesignerView_drop进行更正。

使用sender对象抓取拖动的对象(在此示例按钮中)

void DesignerView_Drop(object sender, DragEventArgs e)
{
    MessageBox.Show("drop");
    Button btn = (Button)sender;

    contentGrid.Children.Add(btn);
}