【问题标题】:GroupBOx Content WPFGroupBOX 内容 WPF
【发布时间】:2011-02-09 05:36:50
【问题描述】:

我有 GroupBox 名称 groupBox1,它包含 StackPanel。在 StackPanel 我有 serevral ChechBoxes。在我的 xaml.cs 中,我想获得那些 CheckBox,但是当我编写下一个代码行时,我得到一个零:

int n = VisualTreeHelper.GetChildrenCount(groupBox1); 

如果我写下一个代码行,我会得到例外:

"指定的索引超出范围或 索引处的子项为空。不要打电话 这个方法如果 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);
    }
}

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);
        }
    }
}

【问题讨论】:

    标签: .net wpf


    【解决方案1】:

    显然,问题在于您试图在元素加载之前获取它的可视子元素。在那一刻,视觉树还没有组成。尝试订阅您的组框的Loaded 事件并在事件处理程序中获取可视子项:

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
    
            Loaded += OnLoaded;
        }
    
        private void OnLoaded(object sender, EventArgs e)
        {
            VisualChildren(grid1);
        }
    
        ...
    }
    

    【讨论】:

    • 好的,谢谢,我会试试的,但是我的 Window 中的 Grid 和其他 StackPanel 是如何以这种方法工作的,我可以使用他们的孩子吗?
    • 如果您发布了 XAML,并且在这两种情况下都试图在哪里获得视觉孩子,这可能有助于诊断问题。
    猜你喜欢
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    相关资源
    最近更新 更多