0

i am trying to create a simple WPF Application which just has comboboxes and a submit button.
what i want to do is,
when i click the button,
my function should loop through all the Comboboxes and get their Content.

My 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>

this is my onclick function

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);
            }
        }

HOW DO I loop through all the comboboxes to get the selected values?? Thanks in adv.

Neil
  • 402
  • 6
  • 18

1 Answers1

0

Assuming that you have all your components in some parent component, you could do this:

foreach(UIElement element in parent.Children)
{
    if(element is ComboBox)
    {
        ComboBox cb = (ComboBox)element;
        ComboBoxItem Item = cb.SelectedItem; 
        allnos.AddLast(Item.Content);
    }
}
npinti
  • 51,780
  • 5
  • 72
  • 96
  • a simple linq query should do all the work : var contents = (from child in Grid.Children.OfType() select ((ComboBoxItem) child.SelectedItem).Content).ToList(); – AymenDaoudi Feb 05 '15 at 14:15