PoshCode Logo PowerShell Code Repository

Format-PoshTable (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1699"></script>download | new post

Format-PoshTable puts the output in a WPF DataGrid (inline in PoshConsole, popup otherwise)

  1. function Format-PoshTable {
  2. #.Synopsis
  3. #  Format-PoshTable puts the output in a WPF DataGrid (inline in PoshConsole)
  4. #.Description
  5. #  Outputs a WPF datagrid of the objects (and properties) specified.
  6. #  This grid can be sorted, rearranged, etc
  7.         [CmdletBinding()]
  8.         param(
  9.       [parameter(ValueFromPipeline=$true)]
  10.       [Array]$InputObject
  11.    ,
  12.       [Parameter(Position=1)]
  13.       [String[]]$Property = "*"
  14.    ,
  15.       [Parameter(Position=2)][Alias("Type")]
  16.       [Type]$BaseType # a type to use as the generic in the collection
  17.    ,
  18.       [Parameter()]
  19.       [Switch]$Popup = (![bool]$Host.PrivateData.WpfConsole)
  20.         )
  21.         Begin
  22.         {
  23.       $global:theFormatPoshTableDataGrid = $null
  24.                 if (!(Get-Command datagrid) )
  25.                 {
  26.                         Import-Module PowerBoots
  27.          Add-BootsFunction 'C:\Program Files (x86)\WPF Toolkit\*\WPFToolkit.dll'
  28.                 }
  29.         }
  30.         Process
  31.         {
  32.       # Create the window here instead of in BEGIN because we need to know the TYPE for the datagrid
  33.                 if(!$global:theFormatPoshTableDataGrid) {
  34.          if(!$BaseType) { $BaseType = $InputObject[0].GetType().FullName }
  35.          # We're going to create a special collection ...
  36.                         $global:ObservableCollection = new-object System.Collections.ObjectModel.ObservableCollection[$BaseType]
  37.          foreach($i in $InputObject) { $ObservableCollection.Add($i) > $null }
  38.          boots {
  39.             Param($ItemCollection, $Property)
  40.                                 datagrid -RowBackground "AliceBlue" -AlternatingRowBackground "LightBlue" -On_AutoGeneratingColumn {
  41.                Param($Source,$SourceEventArgs)
  42.                $header = $SourceEventArgs.Column.Header.ToString()
  43.                $Cancel = $true
  44.                # If it matches any of the properties, don't cancel it.
  45.                foreach($h in $Source.Tag) {  if($header -like $h) {  $Cancel = $false } }
  46.                $SourceEventArgs.Cancel = $Cancel
  47.             }  -ColumnHeaderStyle {
  48.                                         Style -Setters {
  49.                                                 Setter -Property ([System.Windows.Controls.ListView]::FontWeightProperty) -Value ([System.Windows.FontWeights]::ExtraBold)
  50.                                         }
  51.                                 } -ItemsSource $ItemCollection -ov global:theFormatPoshTableDataGrid -tag $Property
  52.          } $ObservableCollection $Property -Inline:(!$Popup) -Popup:$Popup
  53.                 } else {
  54.          @($global:theFormatPoshTableDataGrid)[0].Dispatcher.Invoke( "Normal", ([Action]{  foreach($i in @($InputObject)) { $global:ObservableCollection.Add($i) > $null } }) )  
  55.                 }
  56.         }
  57. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me