如何迭代ContentControls中的图像?
<Canvas x:Name="canvas" >
<ContentControl Style="{StaticResource DesignerItemStyle}">
<Image IsHitTestVisible="True" Source="Media/cross.png" />
</ContentControl>
<ContentControl Style="{StaticResource DesignerItemStyle}">
<Image IsHitTestVisible="True" Source="Media/cross.png" />
</ContentControl>
</Canvas>
我的尝试不起作用:
var ccs = canvas.Children;
foreach (ContentControl c in ccs)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(c); i++)
{
var child = VisualTreeHelper.GetChild(c, i);
}
}
答案 0 :(得分:1)
使用LINQ:
var images = canvas.Children
.OfType<ContentControl>()
.Select(cc => cc.Content as Image)
.Where(img => img != null);
注意,设置IsHitTestVisible="True"
是多余的。 True是默认值。