【问题标题】:Add buttons from a wrap panel to array of buttons将包装面板中的按钮添加到按钮数组
【发布时间】:2017-03-17 09:09:53
【问题描述】:

有没有办法将特定包装面板中的按钮添加到代码中的数组或列表中? 我尝试了下面的代码,但它不起作用:

foreach(Button b in nameOfWrappanel)
{
    list.Add(b);
}

【问题讨论】:

    标签: c# wpf list button wrappanel


    【解决方案1】:

    您必须指定 wrappanel.children 才能访问其子级。

    foreach (Button b in nameOfWrappanel.Children)
    {
        list.Add(b);
    }
    

    【讨论】:

      【解决方案2】:

      你可以使用 Linq:

      var buttons = myWrapPanel.Children.OfType<Button>().ToList();
      

      【讨论】:

        【解决方案3】:

        由于PanelChildren 属性返回可能包含任何类型的UIElement 对象的UIElementCollection,您可以使用OfType LINQ 扩展方法仅检索Button 元素:

        foreach (Button b in nameOfWrappanel.Children.OfType<Button>())
        {
            list.Add(b);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-06-28
          • 1970-01-01
          • 2012-07-22
          • 1970-01-01
          • 1970-01-01
          • 2016-03-05
          • 2013-10-27
          • 1970-01-01
          相关资源
          最近更新 更多