0

我有一个有 400 个孩子的 Canvas。每个孩子都是一个矩形,并用图像填充。如果我想找到某个图像,我会怎么做?

//My code to fill a rectangle
Image img = new Image();
img.Source = new BitmapImage(new Uri(@"hero.png", UriKind.Relative));
img.Margin = rec.Margin;
ImageBrush imgbrush = new ImageBrush();
imgbrush.ImageSource = img.Source;
rec.Fill = imgbrush;

//My attempt at finding that certain rectangle
 foreach (Rectangle rec in canvas1.Children)
        {
            if (rec.Fill = ImageBrush.ImageSourceProperty) // I tried to compare the rectangle with the image's source
            {

            }                
        }
4

2 回答 2

1

您可以使用每个特定的名称,Image然后使用FindName方法

object wantedNode = stackPanel.FindName("dog");
于 2013-03-01T16:29:48.810 回答
0

为什么不将对象存储在以后可以迭代的数组(键/对)中?

于 2013-03-02T01:58:36.710 回答