PoshCode Archive  Artifact [59d20522ec]

Artifact 59d20522ecdbd404651cccbdce2a9b79a0f5afff00234d88b8d3d8ba8f85d231:

  • File Add-GridRow.ps1 — part of check-in [e88b1567d8] at 2018-06-10 13:39:04 on branch trunk — This file was uploaded by a PowerGUI Script Editor Add-on. (user: Anonymous size: 2094)

# encoding: ascii
# api: powershell
# title: Add-GridRow.ps1
# description: This file was uploaded by a PowerGUI Script Editor Add-on.
# version: 0.1
# type: function
# author: Anonymous
# license: CC0
# function: Add-GridRow
# x-poshcode-id: 4285
# x-archived: 2013-07-11T00:33:21
# x-published: 2013-07-02T11:37:00
#
#
function Add-GridRow
{
    <#
        .Synopsis
            Adds a row to an existing grid
        .Description
            Adds a row to an existing grid, and optionally offsets everything past a certain row
        .Example
            Add-GridRow $grid -row Auto,Auto -index 2
        .Parameter grid
            The Grid control to add rows
        .Parameter row
            The row or rows to add to the grid
        .Parameter index
            The offset within the grid 
        .Parameter passThru  
            If set, will output the rows created     
    #>    
    param(
    [Parameter(Mandatory=$true,Position=0)]
    [Windows.Controls.Grid]$grid,
    [Parameter(Mandatory=$true,Position=1)]
    [ValidateScript({
        if (ConvertTo-GridLength $_) {
            return $true
        }
        return $false
    })]
    $row,    
    $index,
    [switch]$passThru
    )    
    
    process {    
        $realRows = @(ConvertTo-GridLength $row)
        foreach ($rr in $realRows) {
            $r = New-Object Windows.Controls.RowDefinition -Property @{
                    Height = $rr
            }
            $null = $grid.RowDefinitions.Add($r)
            if ($passThru) { $r } 
        }
        if ($psBoundParameters.ContainsKey("Index")) {
            foreach ($c in $grid.Children) {
                $controlRow = $c.GetValue([Windows.Controls.Grid]::RowProperty)
                if ($controlRow -ge $index) {
                    $c.SetValue(
                        [Windows.Controls.Grid]::RowProperty, 
                        [Int]($controlRow + $realRows.Count)
                    )                    
                }
            }
        }               
    }
}