Search code examples
c#xamlforeachchildren

XAML .Children property problems


I am trying to filter through my .XAML file and set the ItemSource for numerous ComboBoxes. The issue that I'm having is that when it get to the bottom layer of the foreach loop, my degugger errors out with an obvious message of failed conversion... Here's some code to clarify what the hell I'm talk about:

XAML:

<StackPanel x:Name="spFruit" Orientation="Horizontal" HorizontalAlignment="Left">
    <StackPanel x:Name="spFruit1" Orientation="Vertical" Height="Auto"  Width="250">
        <StackPanel Orientation="Horizontal" Margin="0,2,0,0">
            <CheckBox x:Name="cbApple"  Content="Apples" Margin="0,5"></CheckBox>
            <Label Content="Quantity: " Margin="67,0,0,0"></Label>
            <ComboBox x:Name="cmbxApple"  Margin="10,0,0,0" Width="50" IsEnabled="{Binding ElementName=cbApple, Path=IsChecked}"></ComboBox>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="0,2,0,0">
            <CheckBox x:Name="cbApricot" Content="Apricots" Margin="0,5"></CheckBox>
            <Label Content="Quantity: " Margin="59,0,0,0"></Label>
            <ComboBox x:Name="cmbxApricot" Margin="10,0,0,0" Width="50" IsEnabled="{Binding ElementName=cbApricot, Path=IsChecked}"></ComboBox>
        </StackPanel>

And so forth and so on... (There's thousands of items, and we don't need to go through all of them.) Now, the ComboBoxes... I'm initially trying to populate them with a List<int> (that I've populated already) like so:

C#

foreach (StackPanel spVert in spFruit.Children)
    foreach (StackPanel sp in spVert.Children)
        foreach (ComboBox cmbx in sp.Children)
            cmbx.ItemsSource = lstComboContent; 

Now, I thought (for my specific example) that this should iterate through all the controls within the lowest StackPanel (sp) and find the ComboBoxes, and then assign their ItemSource. Apparently, I was wrong, because when the code executes, it immediately stops on the very first Checkbox and gives me the error that it Cannot Convert System.Windows.Controls.CheckBox to System.Windows.Controls.ComboBox

My official question is this: Why does the loop not just skip over the Checkbox? Obviously , the Checkbox, Label and ComboBox are all children of the StackPanel, and if I tell the loop to look specifically for the ComboBox, why does it stop on (and not continue past) the Checkbox?

I am still pretty new to XAML, so please forgive my ignorance. I thought I had a decent understanding of it until this snag. Seems like a small thing to get hung up on, but here I am. Thanks for all the help and feedback.


Solution

  • The problem is here:

    foreach (ComboBox cmbx in sp.Children)
        cmbx.ItemsSource = lstComboContent; 
    

    StackPanel has 3 childrens - CheckBox, Label, ComboBox. In your for loop you iterate through all children but you try to cast every child to ComboBox. First child is CheckBox and you can't cast it to ComboBox. Your code should be:

    foreach (StackPanel spVert in spFruit.Children)
        foreach (StackPanel sp in spVert.Children)
            foreach (var control in sp.Children)
            {
                if (control.GetType() == typeof (ComboBox))
                {
                    ((ComboBox)control).ItemsSource = lstComboContent;
                }
            }