0

I have some rectangles stored in Canvas.Children, and when traversing the Canvas.Children, I can use

for (int i=0; i<Canvas.Children.Count; i++)
{
     UIElement ui = Canvas.Children[i];
}

However I don't know how to convert ui into System.Windows.Shapes.Rectangle. Could someone help?

C. Wang
  • 2,516
  • 5
  • 29
  • 46

3 Answers3

0

A useful answer can be found in this related question.

Also a useful guide to casting can be found on MSDN here.

Hope this helps.

Community
  • 1
  • 1
Rob Aston
  • 816
  • 12
  • 19
0
if(ui is Rectangle)
{
Rectangle rect = (Rectangle)ui;
}
Valentin Kuzub
  • 11,703
  • 7
  • 56
  • 93
0

This is based on earlier responses. It might be useful for hit boxes, as in collision=hitBox.IntersectsWith(rectList[rcount]);.

Rect[] rectList;
int rcount = 0; 
for (int i=0;i < CanvasB.Children.Count; i++) {
  UIElement ui = CanvasB.Children[i];
  if (ui is Rectangle) {
    rectList[rcount] = new Rect(
      Canvas.GetLeft(ui), 
      Canvas.GetTop(ui), (double)ui.GetValue(ActualWidthProperty), 
      (double)ui.GetValue(ActualHeightProperty)
    );
    rcount++;
  }
}               
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77