循环遍历wpf中的所有组合框

时间:2015-02-05 13:54:44

标签: c# wpf

我正在尝试创建一个简单的WPF应用程序,它只有组合框和提交按钮 我想做的是,
当我点击按钮时,
我的函数应该循环遍历所有的Comboboxes并获取它们的内容
我的XAML -

<ComboBox HorizontalAlignment="Left" Name="ComboBox2"  > // the name goes from 2 to 50
  <ComboBoxItem  IsSelected="True">Y</ComboBoxItem>
  <ComboBoxItem>N</ComboBoxItem>
  <ComboBoxItem>NA</ComboBoxItem>
</ComboBox>

这是我的onclick功能

private void Submit(object sender, RoutedEventArgs e)
        {
            LinkedList<String> allnos = new LinkedList<string>();
            for (int i = 2; i < 12; i++)
            {
                ComboBoxItem Item = (ComboBoxItem)Combobox+"i"+.SelectedItem; // this will not work, how should i get it?
                allnos.AddLast(Item.Content);
            }
        }

我如何遍历所有组合框以获取所选值? 谢谢你。

1 个答案:

答案 0 :(得分:0)

假设您在某个父组件中拥有所有组件,则可以执行以下操作:

foreach(UIElement element in parent.Children)
{
    if(element is ComboBox)
    {
        ComboBox cb = (ComboBox)element;
        ComboBoxItem Item = cb.SelectedItem; 
        allnos.AddLast(Item.Content);
    }
}
相关问题