[英]Add buttons from a wrap panel to array of buttons
Is there a way to add buttons from a specific wrappanel to an array or list in code? 有没有一种方法可以将按钮从特定的包装面板添加到代码中的数组或列表中? I tried the code below, but it doesn't work:
我尝试了下面的代码,但是不起作用:
foreach(Button b in nameOfWrappanel)
{
list.Add(b);
}
You have to specify wrappanel.children to access its child. 您必须指定wrappanel.children才能访问其子级。
foreach (Button b in nameOfWrappanel.Children)
{
list.Add(b);
}
您可以使用Linq:
var buttons = myWrapPanel.Children.OfType<Button>().ToList();
Since the Children
property of a Panel
returns a UIElementCollection
that may contain any type of UIElement
objects, you could use the OfType
LINQ extension method to only retrive the Button
elements: 由于
Panel
的Children
属性返回的UIElementCollection
可能包含任何类型的UIElement
对象,因此可以使用OfType
LINQ扩展方法仅检索Button
元素:
foreach (Button b in nameOfWrappanel.Children.OfType<Button>())
{
list.Add(b);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.