Answer the question
In order to leave comments, you need to log in
How to remove specific Image from Canvas?
It is required to go through all the Image and remove the Image that satisfies the condition.
I am using this code:
foreach (Image img in CnMapField.Children)
{
if (img.Margin.Left == x)
CnMapField.Children.Remove(img);
}
Answer the question
In order to leave comments, you need to log in
Well, for example:
private void DoWork()
{
Canvas CnMapField = new Canvas();
Image[] images = CopyFrom<Image>(CnMapField.Children);
foreach (Image img in images)
{
if (img.Margin.Left == x)
CnMapField.Children.Remove(img);
}
}
private T[] CopyFrom<T>(UIElementCollection collection) where T : UIElement
{
var images = new List<T>(collection.Count);
images.AddRange(collection.Cast<T>());
return images.ToArray();
}
You're modifying the collection you're iterating over - you can't do that. You can use the RemoveAll method:
CnMapField.Children.RemoveAll(img => img.Margin.Left == x);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question