0
我有GroupBox名称groupBox1,它包含StackPanel。在StackPanel中,我有很多ChechBoxes。在我xaml.cs我想这些的CheckBox,但是当我写了下面的代码行,我得到一个零:GroupBOx内容WPF
int n = VisualTreeHelper.GetChildrenCount(groupBox1);
如果我写了下面的代码行我得到exeption:
“指定索引超出范围或 索引处的子项为空。如果VisualChildrenCount为 返回零,表明 Visual没有子项,请不要拨打 参数 name:index实际值为0。
Visual v = (Visual)VisualTreeHelper.GetChild(groupBox1, 0);
这意味着,我的groupBox1没有任何孩子....但什么StackPanel的?
我有这个功能应该itarete过的VisualTree,它看起来像这样:
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);
}
}
它并不需要在groupBox1
的StackPanel
......
谁能告诉我怎样才能到达那些CheckBoxes
?
谢谢。
这是我的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>
,这是代码:
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);
}
}
}
好的,谢谢,我会尝试,但网格和我在我的窗口中的其他StackPanels如何用这种方法工作,我可以使用他们的孩子? – olia 2011-02-08 09:01:19