我有GroupBox名称groupBox1,它包含StackPanel。在StackPanel中,我使用了serevral ChechBoxes。在我的xaml.cs中,我想得到那些CheckBoxes,但是当我编写下一个代码行时,我得到了一个0:
int n = VisualTreeHelper.GetChildrenCount(groupBox1); 如果我编写下一个代码行,就会得到exeption:
“
”指定的索引超出了范围,或者索引处的子索引为空。如果VisualChildrenCount返回零,则不要调用此方法,这表示视觉没有子值。参数名称:索引实际值为0。
Visual v = (Visual)VisualTreeHelper.GetChild(groupBox1, 0);意思是我的groupBox1没有孩子..。但是StackPanel呢?
我有一个函数,它应该位于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);
}
}它不接受StackPanel在groupBox1.
有人能告诉我怎么去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);
}
}
}发布于 2011-02-08 08:55:00
显然,问题在于,在加载元素之前,您正在尝试获取元素的可视子元素。就在那一刻,视觉树还没有形成。尝试订阅组框的Loaded事件,并在事件处理程序中获取可视子事件:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Loaded += OnLoaded;
}
private void OnLoaded(object sender, EventArgs e)
{
VisualChildren(grid1);
}
...
}https://stackoverflow.com/questions/4931069
复制相似问题