2015-02-05 70 views
0

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

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

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

我如何通过所有的组合框获取所选值的循环? 谢谢你。

回答

0

假设你有一些父组件所有组件,你可以这样做:

foreach(UIElement element in parent.Children) 
{ 
    if(element is ComboBox) 
    { 
     ComboBox cb = (ComboBox)element; 
     ComboBoxItem Item = cb.SelectedItem; 
     allnos.AddLast(Item.Content); 
    } 
} 
+0

一个简单的LINQ查询应该做的所有工作: VAR内容=(从孩子Grid.Children。 OfType () select((ComboBoxItem)child.SelectedItem).Content).ToList(); – AymenDaoudi 2015-02-05 14:15:46