olia
olia

Reputation: 1

GroupBOx Content WPF

I have GroupBox name groupBox1 and it contains StackPanel. In the StackPanel I have serevral ChechBoxes. In my xaml.cs I want to get those CheckBoxes but when i write the next code line I get a zero :

int n = VisualTreeHelper.GetChildrenCount(groupBox1); 

If I write the next code line I get exeption :

"Specified index is out of range or child at index is null. Do not call this method if VisualChildrenCount returns zero, indicating that the Visual has no children. Parameter name: index Actual value was 0."

Visual v = (Visual)VisualTreeHelper.GetChild(groupBox1, 0);

It means that my groupBox1 doesn't have any children.... but what about the StackPanel?

I have this function which should itarete over the VisualTree , it looks like this :

private void VisualChildren (Visual myVisual)
{          
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
    {
        Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);

        //Some operations

        VisualChildren(childVisual);
    }
}

It does not take the StackPanel in the groupBox1......

Can anyone please tell me how can i get to those CheckBoxes?
Thank you.


This is my xaml:

<Window x:Class="MyNamespace.MyClass"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test" Height="300" Width="300" Loaded="Window_Loaded" Name="win1">
     <Grid Name="grid1">
        <GroupBox Header="GroupBox" Margin="0,0,95,124" Name="groupBox1">
            <StackPanel Orientation="Vertical" Height="105">
                <CheckBox Height="Auto" Name="checkBox4" Width="Auto" Margin="2">CheckBox</CheckBox>
                <CheckBox Height="Auto" Name="checkBox2" Width="Auto" Margin="2">CheckBox</CheckBox>
                <CheckBox Height="Auto" Name="checkBox3" Width="Auto" Margin="2">CheckBox</CheckBox>
                <CheckBox Height="Auto" Name="checkBox5" Width="Auto" Margin="2">CheckBox</CheckBox>
                <CheckBox Height="Auto" Name="checkBox1" Width="Auto" Margin="2">CheckBox</CheckBox>
                <CheckBox Height="Auto" Name="checkBox6" Width="Auto" Margin="2">CheckBox</CheckBox>
                <CheckBox Height="Auto" Name="checkBox7" Width="Auto" Margin="2">CheckBox</CheckBox>
           </StackPanel>
        </GroupBox>
        <WrapPanel Orientation="Horizontal" Name="wrap2" Margin="8"    HorizontalAlignment="Center" VerticalAlignment="Bottom">
            <Button Margin="5" Height="23"   Name="button1"  VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75">Button</Button>
            <Button Margin="5" Height="23" HorizontalAlignment="Right"  Name="button2" VerticalAlignment="Bottom" Width="75">Button</Button>
            <Button Margin="5" Height="23" HorizontalAlignment="Right"  Name="button3" VerticalAlignment="Top" Width="75">Button</Button>
            <Button Margin="5" Height="23" HorizontalAlignment="Right"  Name="button4" VerticalAlignment="Top" Width="75">Button</Button>
            <Button Margin="5" Height="23" HorizontalAlignment="Right"  Name="button5" VerticalAlignment="Top" Width="75">Button</Button>
            <Button Margin="5" Height="23" HorizontalAlignment="Right"  Name="button6" VerticalAlignment="Top" Width="75">Button</Button>
        </WrapPanel>
    </Grid>
</Window>

and this is the code :

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        try
        {
            VisualChildren(grid1);
        }
    }

    private void VisualChildren(Visual myVisual)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
        {
            Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);
            System.Windows.Forms.MessageBox.Show(childVisual.ToString());
            VisualChildren(childVisual);
        }
    }
}

Upvotes: 0

Views: 1579

Answers (1)

Pavlo Glazkov
Pavlo Glazkov

Reputation: 20756

Apparently the problem is that you are trying to get visual children of an element before it gets loaded. At that moment the visual tree hasn't been composed yet. Try subscribing to the Loaded event of your group box and get the visual children in the event handler:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        Loaded += OnLoaded;
    }

    private void OnLoaded(object sender, EventArgs e)
    {
        VisualChildren(grid1);
    }

    ...
}

Upvotes: 3

Related Questions