我有GroupBox名称groupBox1,它包含StackPanel。在StackPanel我有serevral ChechBoxes。在我的xaml.cs中我想得到那些CheckBoxes但是当我写下一个代码行时我得到一个零:
int n = VisualTreeHelper.GetChildrenCount(groupBox1);
如果我写下一个代码行,我会得到exeption:
“指定的索引超出范围或 index处的child为null。不要打电话 这个方法如果是VisualChildrenCount 返回零,表示该 视觉没有孩子。参数 name:index实际值为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);
}
}
}
答案 0 :(得分:3)
显然问题是你在加载元素之前尝试获取元素的可视子元素。那时,视觉树尚未组成。尝试订阅组框的Loaded
事件,并在事件处理程序中获取可视子项:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Loaded += OnLoaded;
}
private void OnLoaded(object sender, EventArgs e)
{
VisualChildren(grid1);
}
...
}