【发布时间】:2017-03-17 09:09:53
【问题描述】:
有没有办法将特定包装面板中的按钮添加到代码中的数组或列表中? 我尝试了下面的代码,但它不起作用:
foreach(Button b in nameOfWrappanel)
{
list.Add(b);
}
【问题讨论】:
标签: c# wpf list button wrappanel
有没有办法将特定包装面板中的按钮添加到代码中的数组或列表中? 我尝试了下面的代码,但它不起作用:
foreach(Button b in nameOfWrappanel)
{
list.Add(b);
}
【问题讨论】:
标签: c# wpf list button wrappanel
您必须指定 wrappanel.children 才能访问其子级。
foreach (Button b in nameOfWrappanel.Children)
{
list.Add(b);
}
【讨论】:
你可以使用 Linq:
var buttons = myWrapPanel.Children.OfType<Button>().ToList();
【讨论】:
由于Panel 的Children 属性返回可能包含任何类型的UIElement 对象的UIElementCollection,您可以使用OfType LINQ 扩展方法仅检索Button 元素:
foreach (Button b in nameOfWrappanel.Children.OfType<Button>())
{
list.Add(b);
}
【讨论】: