GroupBoxの名前はgroupBox1で、StackPanelが含まれています。 StackPanelには、私はセクシャルChechBoxesを持っています。指定された「GroupBOxコンテンツWPF
:私のxaml.csで、私は、これらのチェックボックスを取得したいが、私は次のコード行を書くとき、私はゼロを得る:
int n = VisualTreeHelper.GetChildrenCount(groupBox1);
私は次のコード行を記述する場合、私はexeptionを取得しますインデックスが範囲外または インデックスの子がnullの場合 VisualChildrenCount が0を返し、 Visualに子がないことを示す場合、このメソッドを呼び出さないでください。パラメータ name:index実際の値は0です。
Visual v = (Visual)VisualTreeHelper.GetChild(groupBox1, 0);
それは私のgroupBox1が子を持っている....が、何のStackPanelについていないことを意味しますか?
私はVisualTree上itarete必要があり、この機能を持って、それは次のようになります。
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);
}
}
}
[OK]を、おかげで、私はそれをしようとしますが、グリッドと私は私の窓を持っている他のStackPanelsはありますかこの方法で働くと私は彼らの子供を使用することができますか? – olia
XAMLを投稿した場合、および両方の場合に視覚的な子を取得しようとしている場合は、問題の診断に役立ちます。 –