0

我创建了一个可以通过颜色组合框填充的椭圆对象。现在我需要能够单击(并按住)圆圈,将光标拖到画布上,当我释放鼠标按钮时,应该在该位置绘制一个新圆圈,其尺寸和颜色与我单击的圆圈相同。

我认为我在 Drop 事件中做错了什么。

    private Ellipse dragCircle = new Ellipse();

    private void eBallColor_MouseMove(object sender, MouseEventArgs e)
    {
        dragCircle = (Ellipse)sender;
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            DataObject dragColor = new DataObject("theColor", dragCircle.Fill);
            DragDrop.DoDragDrop(dragCircle, dragColor, DragDropEffects.Move);
        }
    }

    private void Ellipse_Drop(object sender, DragEventArgs e)
    {
        Ellipse circle = (Ellipse)sender;
        if (e.Data.GetDataPresent("theColor"))
        {
            Brush draggedColor = (Brush)e.Data.GetData("theColor");
            circle.Fill = draggedColor;
            dragCircle.Fill = Brushes.White;
            Point pos = e.GetPosition(canvasCard);
            double posX = pos.X;
            double posY = pos.Y;
            Canvas.SetLeft(dragCircle, posX);
            Canvas.SetTop(dragCircle, posY);
            canvasCard.Children.Add(dragCircle);
        }
    }
4

0 回答 0