简体   繁体   English

逐步浏览包装面板中的控件

[英]Stepping through controls in a wrappanel

I have a bunch of controls located in a wrappanel in a WPF app that are procedurally created. 我在程序创建的WPF应用程序的包装面板中有一堆控件。 The first control is a label followed by a bunch of comboboxes and a checkbox. 第一个控件是一个标签,后跟一堆组合框和一个复选框。 The user clicks a button and a new row of controls are added. 用户单击一个按钮,然后添加新的控件行。 This worked fine. 这很好。 Then I decided to make the label a bit more attractive by giving it a red circle as a background and the label was nested in a grid with the red circel behind the label that simply listed the row number. 然后,我决定通过给它一个红色圆圈作为背景使该标签更具吸引力,并将该标签嵌套在一个网格中,在该标签后面的红色圆圈仅列出行号。 This worked fine. 这很好。 And I use to step through all the controls with this block: 我经常使用此代码块浏览所有控件:

 foreach (Control item in WrapPanelItems.Children)
            {
                if (item.GetType() == typeof(CheckBox))
                {
                    RowCounter++;
                }
            }

now suddenly this block of code fails with this error: 'Unable to cast object of type 'System.Windows.Controls.Grid' to type 'System.Windows.Controls.Control'.' 现在,此代码块突然因以下错误而失败:“无法转换类型为“ System.Windows.Controls.Grid”的对象以类型为“ System.Windows.Controls.Control”。”

So I suspect the grid isnt a conventional item and the code fails. 因此,我怀疑网格不是常规项目,并且代码失败。 But how would I then iterate through the controls without the app crashing and still iterate through all the conventional controls ontop of it? 但是,我又该如何遍历控件而又不会使应用程序崩溃,而仍然遍历其顶部的所有常规控件?

Here is the code for how I style and add the label. 这是我如何设置样式和添加标签的代码。

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

And also a second question. 还有第二个问题。 Suppose I want to change the text on the label... how would I get to the label if its nested in a grid? 假设我想更改标签上的文本...如果标签嵌套在网格中,我将如何到达标签? Can you just change the content directly when the FOR loop picks up the grid? 您可以直接在FOR循环拾取网格时直接更改内容吗? Or should you then say all children of grid when the grid is identified in the FOR loop? 还是在FOR循环中识别出网格后再说网格的所有子级?

tx TX

Grid is not a System.Windows.Controls.Control so this line throws an exception: Grid不是System.Windows.Controls.Control因此此行引发异常:

foreach (Control item in WrapPanelItems.Children)

You can replace Control with UIElement to avoid this error. 您可以将Control替换为UIElement以避免此错误。


According to MSDN : 根据MSDN

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM