C# MouseDragElementBehavior留下重复项

C# MouseDragElementBehavior留下重复项,c#,wpf,drag-and-drop,C#,Wpf,Drag And Drop,我对WPF中的MouseDragElementBehvior有点问题。我正在实现一个拼字游戏,我想把它从一个架子上拖到黑板上,这两个都是画布元素。瓷砖每次都明显不同,因此在代码中生成 我成功地使拖动行为正常工作,并且我能够将图像从tile rack画布拖动到另一个画布。当我把一个放在板上并希望再次移动它时,问题就出现了。它在最初几次工作,有时,当您拖动已在板上的项目时,它会留下该项目的副本,然后抛出空引用异常 我实现它的方法是使用一个全局图像变量来跟踪当前正在拖动的图像(如果有),基本上在Mou

我对WPF中的MouseDragElementBehvior有点问题。我正在实现一个拼字游戏,我想把它从一个架子上拖到黑板上,这两个都是画布元素。瓷砖每次都明显不同,因此在代码中生成

我成功地使拖动行为正常工作,并且我能够将图像从tile rack画布拖动到另一个画布。当我把一个放在板上并希望再次移动它时,问题就出现了。它在最初几次工作,有时,当您拖动已在板上的项目时,它会留下该项目的副本,然后抛出空引用异常

我实现它的方法是使用一个全局图像变量来跟踪当前正在拖动的图像(如果有),基本上在MouseDown事件中,我将当前选择的平铺更改为刚刚按下的平铺

 MouseDragElementBehavior dragBe = new MouseDragElementBehavior();
 dragBe.Attach(imageIcon);
 dragBe.DragFinished += onDragFinishedFromBoard;                      
 imageIcon.MouseDown += (sender, args) =>
 {
 if (currentTileSelected ==null)
 {
        currentTileSelected = sender as Image;                              
 } };
然后为拖动完成触发的事件:

    private void onDragFinishedFromBoard(object sender, MouseEventArgs args)
{
if (currentTileSelected != null)
        {
            s = (Square) currentTileSelected.Tag;
            letter = (Tile)s.Letter;

            double X = args.GetPosition(canvasBoard).X;
            double y = args.GetPosition(canvasBoard).Y;
            int row, col;
            if (X > 0 && y > 0)
            {

                row = (int)Math.Floor(y / 35);
                col = (int)Math.Floor(X / 35);
                if (theCurrentGame.TheBoard.ScrabbleBoard[col][row].ContainsLetter==true)
                {
                    //
                    currentTileSelected.Source = null;
                    currentTileSelected.Visibility = Visibility.Collapsed;
                    currentTileSelected = null;
                    redrawTileRack();


                }
                else
                {
                    Debug.WriteLine("row: " + row + " col:" + col);
                    //remove it from old position
                    if (s!=null)
                    {
                        s.ContainsLetter = false;
                        s.Letter = null;
                        s.partOfCurrentTurn = false;
                        theCurrentGame.TheBoard.ScrabbleBoard[s.Coordinate.Key][s.Coordinate.Value] = s;

                    }
                   //snap it into board new position
                    Square currentSquare = theCurrentGame.TheBoard.ScrabbleBoard[col][row];
                    currentSquare.ContainsLetter = true;
                    currentSquare.Letter = letter;
                    currentSquare.partOfCurrentTurn = true;
                    theCurrentGame.TheBoard.ScrabbleBoard[col][row] = currentSquare;
                    drawBoard(theCurrentGame.TheBoard);
                    //remove tile from display
                    theCurrentGame.Human.TileRack.Tiles.Remove(letter);
                    currentTileSelected.Source = null;
                    currentTileSelected.Visibility = Visibility.Collapsed;
                   currentTileSelected = null;
                }
            }
            else
            {
                currentTileSelected.Source = null;
                currentTileSelected.Visibility = Visibility.Collapsed;
              currentTileSelected= null;
                redrawTileRack();
            }

        }

有谁能帮我解释一下为什么会发生这种情况,元素被拖拽,但留下一个副本,然后抛出一个空引用异常?或者建议一种更好、更可靠的方法来实现这一点。

如果有人想知道我是如何解决这个问题的,我忘了在再次绘制画布之前基本上清理画布。所需要的只是在再次绘制之前删除所有子元素

我使用了一个名为的工具,它向我展示了不同的层,然后我设法弄清楚我的代码在做什么