WPF Stackpanel孩子无法选择使用foreach

时间:2018-07-25 08:21:54

标签: wpf

GUI使用以下代码动态创建:

Grid gridBox = new Grid();
Grid gridBgm = new Grid();
StackPanel stack = new StackPanel();
stack.Children.Add(lbl);
stack.Children.Add(comboxBoxs);
stack.Children.Add(tgglBtn);
gridBox.Children.Add(borders);
gridBox.Children.Add(stack);

// add button into grid
Grid.SetColumn(gridBox, x);
Grid.SetRow(gridBox, y);
gridBgm.Children.Add(gridBox);

输出GUI:

Output GUI

下面的foreach代码在选择当前组合框值时无法检查当前切换按钮。

ComboBox cbb = sender as ComboBox;
var gBoxChild = gridBox.Children.OfType<StackPanel>();

foreach (StackPanel c in gBoxChild)
{
    if (c.GetType() == typeof(StackPanel))
    {
        foreach (Control ctrl in c.Children)
        {
            if (ctrl.GetType() == typeof(ToggleButton))
            {
                if (cbb.SelectedIndex == 0)
                {
                    ((ToggleButton)c).IsChecked = true;
                }
            }
        }
    }
}

0 个答案:

没有答案
相关问题