如何从堆栈面板中删除图像子项

问题描述

我有一个 Stackpanel,里面有 7 个图像,每个图像代表一个多米诺牌。当用户将图像拖放到名为 Board 的画布中时,该图像应该从 Stackpanel 中消失。有关更多上下文,这是 xaml 中的代码

<Canvas x:Name="Board" Grid.Row="2" AllowDrop="True" Background="Transparent" Drop="DropImage"/>

<StackPanel x:Name="TilesPlayer1" Grid.Row="3" AllowDrop="True" Orientation="Horizontal" HorizontalAlignment="Center" Margin="10" />

多米诺牌在 cs 内部的一个方法添加到 Stackpanel 中,因此没有名称或标识符。这是我必须拖动它们的代码,它可以工作,但我有几个问题。这是 C# 代码

private void DragImage(object sender,MouseButtonEventArgs e)
    {
        Image image = e.source as Image;
        DataObject data = new DataObject(typeof(ImageSource),image.source);
        DragDrop.DoDragDrop(image,data,DragDropEffects.Move);
    }

    private void DropImage(object sender,DragEventArgs e)
    {
        ImageSource image = e.Data.GetData(typeof(ImageSource)) as ImageSource;

        Image imageControl = new Image() { Width = image.Width,Height = image.Height,Source = image };
        Canvas.SetLeft(imageControl,e.GetPosition(Board).X);
        Canvas.SetTop(imageControl,e.GetPosition(Board).Y);
        Board.Children.Add(imageControl);
    }

代码有效,但不是我想要的。

  • 它允许我将我想要的图像拖到画布上,但不考虑大小,如果我尝试以另一种方式处理它,图像不会出现在画布内。
  • 我无法纠正的主要问题是图像被复制,但我无法从 Stackpanel 中删除它。我调查并发现,因为它是一个图像,所以我无法删除它,因为要将它从 Stackpanel 中删除,它必须是一个 UIElement,我找不到如何解决它。

希望你能帮我。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)