删除添加到画布的所有图像

时间:2022-11-04 09:57:50

Is there a possible way to remove (delete) all images (children) added to a Canvas in C# (in WFP)?

有没有办法删除(删除)添加到C#中的Canvas(在WFP中)的所有图像(子)?

2 个解决方案

#1


35  

Do you mean you just want to remove all the child elements?

你的意思是你只想删除所有子元素?

canvas.Children.Clear();

looks like it should do the job.

看起来它应该做的工作。

EDIT: If you only want to remove the Image elements, you can use:

编辑:如果您只想删除图像元素,您可以使用:

var images = canvas.Children.OfType<Image>().ToList();
foreach (var image in images)
{
    canvas.Children.Remove(image);
}

This assumes all the images are direct child elements though - if you want to remove Image elements under other elements, it becomes trickier.

这假设所有图像都是直接的子元素 - 如果要删除其他元素下的图像元素,它会变得更加棘手。

#2


6  

Since the children collection of a Canvas is a UIElementCollection and there are plenty of other controls that use this type of collection, we could add remove method to all of them with an extension method.

由于Canvas的子集合是一个UIElementCollection,并且有很多其他控件使用这种类型的集合,我们可以使用扩展方法为所有这些集合添加remove方法。

public static class CanvasExtensions
{
    /// <summary>
    /// Removes all instances of a type of object from the children collection.
    /// </summary>
    /// <typeparam name="T">The type of object you want to remove.</typeparam>
    /// <param name="targetCollection">A reference to the canvas you want items removed from.</param>
    public static void Remove<T>(this UIElementCollection targetCollection)
    {
        // This will loop to the end of the children collection.
        int index = 0;

        // Loop over every element in the children collection.
        while (index < targetCollection.Count)
        {
            // Remove the item if it's of type T
            if (targetCollection[index] is T)
                targetCollection.RemoveAt(index);
            else
                index++;
        }
    }
}

When this class is present you can simply remove all images (or any other type of object) with the line.

当此类存在时,您可以使用该行删除所有图像(或任何其他类型的对象)。

testCanvas.Children.Remove<Image>();

#1


35  

Do you mean you just want to remove all the child elements?

你的意思是你只想删除所有子元素?

canvas.Children.Clear();

looks like it should do the job.

看起来它应该做的工作。

EDIT: If you only want to remove the Image elements, you can use:

编辑:如果您只想删除图像元素,您可以使用:

var images = canvas.Children.OfType<Image>().ToList();
foreach (var image in images)
{
    canvas.Children.Remove(image);
}

This assumes all the images are direct child elements though - if you want to remove Image elements under other elements, it becomes trickier.

这假设所有图像都是直接的子元素 - 如果要删除其他元素下的图像元素,它会变得更加棘手。

#2


6  

Since the children collection of a Canvas is a UIElementCollection and there are plenty of other controls that use this type of collection, we could add remove method to all of them with an extension method.

由于Canvas的子集合是一个UIElementCollection,并且有很多其他控件使用这种类型的集合,我们可以使用扩展方法为所有这些集合添加remove方法。

public static class CanvasExtensions
{
    /// <summary>
    /// Removes all instances of a type of object from the children collection.
    /// </summary>
    /// <typeparam name="T">The type of object you want to remove.</typeparam>
    /// <param name="targetCollection">A reference to the canvas you want items removed from.</param>
    public static void Remove<T>(this UIElementCollection targetCollection)
    {
        // This will loop to the end of the children collection.
        int index = 0;

        // Loop over every element in the children collection.
        while (index < targetCollection.Count)
        {
            // Remove the item if it's of type T
            if (targetCollection[index] is T)
                targetCollection.RemoveAt(index);
            else
                index++;
        }
    }
}

When this class is present you can simply remove all images (or any other type of object) with the line.

当此类存在时,您可以使用该行删除所有图像(或任何其他类型的对象)。

testCanvas.Children.Remove<Image>();