Stepping through controls in a wrappanel











up vote
0
down vote

favorite












I have a bunch of controls located in a wrappanel in a WPF app that are procedurally created. 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'.'



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? Or should you then say all children of grid when the grid is identified in the FOR loop?



tx










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have a bunch of controls located in a wrappanel in a WPF app that are procedurally created. 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'.'



    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? Or should you then say all children of grid when the grid is identified in the FOR loop?



    tx










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a bunch of controls located in a wrappanel in a WPF app that are procedurally created. 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'.'



      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? Or should you then say all children of grid when the grid is identified in the FOR loop?



      tx










      share|improve this question













      I have a bunch of controls located in a wrappanel in a WPF app that are procedurally created. 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'.'



      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? Or should you then say all children of grid when the grid is identified in the FOR loop?



      tx







      c# wpf






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 at 18:26









      Breytenzest

      113




      113
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Grid is not a System.Windows.Controls.Control so this line throws an exception:



          foreach (Control item in WrapPanelItems.Children)


          You can replace Control with UIElement to avoid this error.





          According to MSDN:



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





          share|improve this answer





















          • thank you it worked like a charm!
            – Breytenzest
            Nov 15 at 21:49











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325752%2fstepping-through-controls-in-a-wrappanel%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          Grid is not a System.Windows.Controls.Control so this line throws an exception:



          foreach (Control item in WrapPanelItems.Children)


          You can replace Control with UIElement to avoid this error.





          According to MSDN:



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





          share|improve this answer





















          • thank you it worked like a charm!
            – Breytenzest
            Nov 15 at 21:49















          up vote
          0
          down vote



          accepted










          Grid is not a System.Windows.Controls.Control so this line throws an exception:



          foreach (Control item in WrapPanelItems.Children)


          You can replace Control with UIElement to avoid this error.





          According to MSDN:



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





          share|improve this answer





















          • thank you it worked like a charm!
            – Breytenzest
            Nov 15 at 21:49













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Grid is not a System.Windows.Controls.Control so this line throws an exception:



          foreach (Control item in WrapPanelItems.Children)


          You can replace Control with UIElement to avoid this error.





          According to MSDN:



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





          share|improve this answer












          Grid is not a System.Windows.Controls.Control so this line throws an exception:



          foreach (Control item in WrapPanelItems.Children)


          You can replace Control with UIElement to avoid this error.





          According to MSDN:



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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 at 18:35









          Bizhan

          7,74163255




          7,74163255












          • thank you it worked like a charm!
            – Breytenzest
            Nov 15 at 21:49


















          • thank you it worked like a charm!
            – Breytenzest
            Nov 15 at 21:49
















          thank you it worked like a charm!
          – Breytenzest
          Nov 15 at 21:49




          thank you it worked like a charm!
          – Breytenzest
          Nov 15 at 21:49


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325752%2fstepping-through-controls-in-a-wrappanel%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?