在 vb.net 的代码中更改大量形状的填充

Change fills of a lot of shapes in code in vb.net

这次我正在开发一个 windows 商店应用程序,当我试图在代码中更改大量矩形的填充时遇到了问题。形状太多无法单独命名并单独更改它们的填充(至少有 150 个矩形)。它们被放置在 canvas 中,但是 Canvas.Children 给了我一个 UIElement 值,这不是我想要的(我想要 Xaml.Shapes.Rectangle,我可以更改它们的填充)。那么,有什么想法吗?非常感谢。

大卫, 2015-08-27

试试这个:

(编辑:刚注意到 vb.net 标签;这段代码是用 C# 写的。但想法是遍历 Canvas.Children 中的所有元素并检查它是否是矩形,如果是然后改变它的填充)

foreach(UIElement child in MyCanvas.Children)
        {
            if(child.GetType()== typeof(Windows.UI.Xaml.Shapes.Rectangle))
            {
                ((Rectangle)child).Fill = new SolidColorBrush(Windows.UI.Colors.AliceBlue);
            }
        }