Loop through WPF controls in Powershell

Welcome Forums General PowerShell Q&A Loop through WPF controls in Powershell

Viewing 1 reply thread
  • Author
    Posts
    • #195995
      Participant
      Topics: 1
      Replies: 0
      Points: 13
      Rank: Member

      Hi,

      I am attempting to loop through all the controls used in PS via a WPF form.  At this point I am just trying to get all the controls recognised but it it does not seem to be seeing all the children

       

      I have a stackpanel – WPF code here – not the complete code of the project






























       

      I have a function that I call using Recursive $spBasicDRVSTART

      Function Recursive ($Control)
      {
      $Controls = $Control.Children
      foreach ($ctl in $Controls)
      {
      Write-Host $ctl.Name
      if ($ctl.HasChildren)
      {
      Recursive $ctl
      }
      }
      }

      The function outputs the direct children of the stackpanel but not the children of the children.

      e.g.

      lblPullService is written to the console but chkPullService is not.

      I need to be able to do some checks on the control e.g. is it a checkbox control but I do not seem to be getting to it.

      Any assistance on what I am doing wrong is appreciated.

    • #196076
      Participant
      Topics: 10
      Replies: 1381
      Points: 1,509
      Helping Hand
      Rank: Community Hero

      This is more of a .NET question than Powershell, so you may want to start with searching along the lines of '.Net Controls Enumerate' or something like that. A portion of your post appears to be missing, but the question is what are passing to the function as Control? The below is using an entire page or form and then emumerating the controls under the 'global' controls:

      https://forums.asp.net/t/1282048.aspx?How+to+programmatically+loop+through+all+controls+on+a+page+

      One thing that does not appear to be correct is:

      $Controls = $Control.Children
      

      This is taking the control you are passing and getting the Children, not enumerating the controls directly under the object you are passing. The Recursion should be doing this, you shouldn't need to reference .Children. Something like this:

      Function Get-Control ($Control) {
          foreach ($ctl in $Control.Controls) {
              Write-Host $ctl.Name
              if ($ctl.HasChildren){
                  Get-Control -Control $ctl
              }
          }
      }
      
Viewing 1 reply thread
  • You must be logged in to reply to this topic.