Get-ChildControl cant find control in non-active tabItem

Nov 11, 2011 at 9:53 AM

Get-ChildControl cant find control in non-active tabItem, and I see thet powershell variable for named control is not created also, I dont know is there other way to do this.

My concrete situation is that I want to read values of checkbox in other tab.

I looked at source of Get-ChildControl and it uses VisualTreeHelper classs to find child controls, but it is a known issue that it doesnt work : http://forums.silverlight.net/p/85766/199159.aspx
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/693abf03-5042-4ab6-86e8-76aa24a496e9/.

I am system admin and new to WPF but I skimed articel or two and maybe you could use LogicalTreeHelper in Get-ChildControl ?

Nov 11, 2011 at 10:20 AM

I tried to modify Get-ChildControl myself to use LogicalTree, and it works for my script, and now even works without calling Get-ChildControl,  I really get powershell variable named after WPF control.

Here is modified Get-ChildControl :

function Get-ChildControl
{
    <#
    .Synopsis
        Imports variables to interact with a control's children
    .Description
        
    #>
    param(
    [Parameter(ValueFromPipeline=$true, Mandatory=$false)]
    [Alias('Tree')]
    $Control = $Window,
    [Parameter(Position=0)][string[]]$ByName,

    [Switch]$OnlyDirectChildren,       
    [string[]]$ByControlName,
    [Type[]]$ByType,
    [string]$ByUid,
    [String]$GetProperty,        
    [switch]$OutputNamedControl,
    [Switch]$PeekIntoNestedControl
    )
    
  
    process {
        if ($byUid) { $PeekIntoNestedControl = $true } 
        $hasEnumeratedChildren = $false
        if (-not $Control) { return }
        $namedNestedControls = @{}
        $queue = New-Object Collections.Generic.Queue[System.Windows.FrameworkElement]
        $queue.Enqueue($control)
        $hasOutputtedSomething = $false
        while ($queue.count) {
            $parent = $queue.Peek()
            
            if ('ShowUI.ShowUISetting' -as [type]) {
                $controlname = $parent.GetValue([ShowUI.ShowUISetting]::ControlNameProperty)
            } else {
                $controlname = ""
            }
            
            if ($parent.Name) {
                $namedNestedControls[$parent.Name] = $parent
            }
            
            if (-not $OutputNamedControl) {
                if ($getProperty){
                    $__propertyExistsOnObject = $parent.psObject.Properties[$getProperty]
                    if ($__PropertyExistsOnObject) {
                        $parent.$getProperty
                    }
                } elseif ($byName) {
                    if ($ByName -contains $parent.Name) { 
                        $hasOutputtedSomething  = $true
                        $parent 
                    } 
                } elseif ($byControlName) {
                    if ($byControlName -contains $controlname) { 
                        $hasOutputtedSomething = $true
                        $parent 
                    } 
                } elseif ($ByType) {
                    foreach ($bt in $byType) {
                        if ($parent.GetType() -eq $bt -or 
                            $parent.GetType().IsSubclassOf($bt)) { 
                            $hasOutputtedSomething = $true
                            $parent 
                        } 
                    }
                } elseif ($byUid) {
                    if ($parent.Uid -eq $uid) { 
                        $hasOutputtedSomething = $true
                        $parent 
                    }
                } else {                    
                    if ((-not $hasOutputtedSomething) -and $OnlyDirectChildren) {
                        # When -OnlyDirectChildren is specified, the first item
                        # out would be the parent, so skip that
                        $hasOutputtedSomething = $true                        
                    } else {
                        $hasOutputtedSomething = $true                        
                        $parent                
                    }
                    
                }
            }
            
                       
            $children_x = [System.Windows.LogicalTreeHelper]::GetChildren($parent)
            
            
            #write-host "PARENT:" $parent "--" $parent.Name "COUNT:" $childCount
            
            $shouldEnumerateChildren = $false            
            
            if ($children_x) {            
                if (-not ($hasEnumeratedChildren -and $OnlyDirectChildren)) {
                    if ((-not $HasEnumeratedChildren) -or                 
                        (-not $controlname -or $PeekIntoNestedControl)) {
                        $hasEnumeratedChildren = $true
                        foreach ($ch in $children_x) {
                            $child = $ch
                            
                            #write-host "     CHILD:" $child "--" $child.Name
                            if ($child -is [System.Windows.FrameworkElement]) { $queue.Enqueue($child) }
                        }            
                    }                                        
                }
            } else {
                if ($parent -is [Windows.Controls.ContentControl]) {
                    $child = $parent.Content
                    
                    if ($child -and $child -is [System.Windows.FrameworkElement]) {
                        $hasEnumeratedChildren = $true
                        $queue.Enqueue($child)
                    } else {
                        if (-not $outputNamedControl -and
                            -not $byType -and
                            -not $byName -and
                            -not $byUid -and 
                            -not $byControlName) {
                            $hasEnumeratedChildren = $true
                            $child
                        }
                        
                    }
                }
            }
            
            $parent = $queue.Dequeue() 
        }

        if ($OutputNamedControl) {
            $namedNestedControls
        }                                               
    }      
}
   

Coordinator
Nov 12, 2011 at 4:51 AM

Thanks, that's awesome. I'll have a look and make sure we fix that in the next release :)