PoshCode Archive  Artifact [a64a5911ff]

Artifact a64a5911ff3973fd26abe1a8136e93b1c4f7330570c433830a1b27b3fa538ccb:

  • File Boots-DataGrid-Binding.ps1 — part of check-in [08adb241ef] at 2018-06-10 13:07:52 on branch trunk — Bind DataGrid to an object array using Powerboots (user: foureight84 size: 2287)

# encoding: ascii
# api: powershell
# title: Boots DataGrid Binding
# description: Bind DataGrid to an object array using Powerboots
# version: 0.1
# type: module
# author: foureight84
# license: CC0
# function: Export-NamedControl
# x-poshcode-id: 2259
# x-archived: 2015-10-24T23:47:53
# x-published: 2010-09-21T16:14:00
#
# Wpf DataGrid requires .net 4 or wpftoolkit if you haven’t installed.
# Requires Xaml layout from http://poshcode.org/2256; ConvertFrom-Hashtable (older version by Jaykul, although I think v2 works the same) http://poshcode.org/1118; and Export-NamedControl (included) (also written by Jaykul for Boots)
#
#load boots#
Import-Module Powerboots


function Export-NamedControl {
	[CmdletBinding()]
	param(
	   [Parameter(ValueFromPipeline=$true, Position=1, Mandatory=$true)]
	   $Root = $BootsWindow
	)
	process {
	   Invoke-BootsWindow $Root { 
		  $control = $BootsWindow 
		  while($control) {
			 $control = $control | ForEach-Object {
				$Element = $_
				if(!$Element) { return }
	   
				Write-Verbose "This $($Element.GetType().Name) is $Element"
	  
				if($Element.Name) {
				   Write-Verbose "Defining $($Element.Name) = $Element"
				   Set-Variable "$($Element.Name)" $Element -Scope 2
				}
				## Return all the child controls ...
				@($Element.Children) + @($Element.Child) + @($Element.Content) + 
				@($Element.Items) + @($Element.Inlines) + @($Element.Blocks)
			 }
		  }
	   }
	}
}

function ConvertFrom-Hashtable {
PARAM([HashTable]$hashtable,[switch]$combine)
BEGIN { $output = New-Object PSObject }
PROCESS {
if($_) { 
   $hashtable = $_;
   if(!$combine) {
      $output = New-Object PSObject
   }
}
   $hashtable.GetEnumerator() | 
      ForEach-Object { Add-Member -inputObject $output `
	  	-memberType NoteProperty -name $_.Name -value $_.Value }
   $output
}
}

#--- Boots ---#
$window = New-BootsWindow {} -FileTemplate "[your_path to xaml_file.xaml]" -async -Passthru -On_Loaded {
    Export-NamedControl -Root $Args[0]
	$data = @{ 
		DeviceGroup = "Samsung" 
		Device = "SGH-A887" 
		Platform = "J2ME" 
	}, @{ 
		DeviceGroup = "Motorola"
		Device = "V3i"
		Platform = "J2ME"
	}   | ConvertFrom-Hashtable
	
	$HadesDevices.ItemsSource = $data
}