Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在wrappanel中单步执行控件_C#_Wpf - Fatal编程技术网

C# 在wrappanel中单步执行控件

C# 在wrappanel中单步执行控件,c#,wpf,C#,Wpf,我在WPF应用程序的wrappanel中有一组控件,它们是按程序创建的。第一个控件是一个标签,后面是一组组合框和一个复选框。用户单击一个按钮并添加一行新的控件。这很有效。然后我决定给标签一个红色的圆圈作为背景,让它更吸引人,标签被嵌套在一个网格中,红色圆圈在标签后面,只是列出了行号。这很有效。我使用此块逐步浏览所有控件: foreach (Control item in WrapPanelItems.Children) { if (ite

我在WPF应用程序的wrappanel中有一组控件,它们是按程序创建的。第一个控件是一个标签,后面是一组组合框和一个复选框。用户单击一个按钮并添加一行新的控件。这很有效。然后我决定给标签一个红色的圆圈作为背景,让它更吸引人,标签被嵌套在一个网格中,红色圆圈在标签后面,只是列出了行号。这很有效。我使用此块逐步浏览所有控件:

 foreach (Control item in WrapPanelItems.Children)
            {
                if (item.GetType() == typeof(CheckBox))
                {
                    RowCounter++;
                }
            }
现在,这段代码突然失败,出现以下错误:“无法将“System.Windows.Controls.Grid”类型的对象强制转换为“System.Windows.Controls.Control”

所以我怀疑网格不是一个常规项目,代码失败了。但是,我如何在应用程序不崩溃的情况下迭代这些控件,并且仍然在上面迭代所有常规控件呢

下面是我如何设置样式和添加标签的代码

        Grid MyGrid= new Grid();

        Ellipse myEllipse = new Ellipse();

        SolidColorBrush mySolidColorBrush = new SolidColorBrush();

        mySolidColorBrush.Color = Color.FromArgb(255, 107, 142, 35);

        myEllipse.Fill = mySolidColorBrush;

        myEllipse.Width = 20;
        myEllipse.Height = 20;

        MyGrid.Children.Add(myEllipse);

        Label LabelCounter = new Label();
        LabelCounter.Content = RowCount.ToString();

        MyGrid.Children.Add(LabelCounter);
        LabelCounter.VerticalAlignment = VerticalAlignment.Center;
        LabelCounter.HorizontalAlignment = HorizontalAlignment.Center;

        WrapPanelItems.Children.Add(MyGrid);
还有第二个问题。假设我想更改标签上的文本。。。如果标签嵌套在网格中,我如何找到它?当FOR循环拾取网格时,您可以直接更改内容吗?或者当网格在FOR循环中被标识时,您应该说它是网格的所有子级吗


tx

网格
不是
系统.Windows.Controls.Control
,因此此行引发异常:

foreach (Control item in WrapPanelItems.Children)
您可以将
控件
替换为
ui元素
,以避免此错误


根据:

public class Grid : System.Windows.Controls.Panel, System.Windows.Markup.IAddChild