簡體   English   中英

如何將 UIElement 轉換為矩形

[英]How to cast UIElement to Rectangle

我在 Canvas.Children 中存儲了一些矩形,在遍歷 Canvas.Children 時,我可以使用

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

但是我不知道如何將 ui 轉換為 System.Windows.Shapes.Rectangle。 有人可以幫忙嗎?

在這個相關問題中可以找到一個有用的答案。

也可以在 MSDN此處找到有關轉換的有用指南。

希望這可以幫助。

if(ui is Rectangle)
{
Rectangle rect = (Rectangle)ui;
}

這是基於之前的回答。 它可能對命中框很有用,如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++;
  }
}               

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM