Werbung

Herkunft

Kurzinformation

Converts the UIElement to a data template

Syntax

<# .Synopsis Converts the UIElement to a data template .Description Converts the UIElement to a data template by stripping its resources, outputting the control as XAML, and enclosing within tags .Example New-Image | ConvertTo-DataTemplate @{"Source" = "MySource"} -outputXaml .Example New-ListBox -ItemsSource { Get-Process } -ItemTemplate { New-StackPanel -Orientation Horizontal -Children { New-Label -Name ProcessName -FontSize 14 New-Label -Name Id -FontSize 8 } | ConvertTo-DataTemplate -binding @{ "ProcessName.Content" = "ProcessName" "Id.Content" = "Id" } } -show .Parameter control The UIElement to turn into a data .Parameter binding A dictionary of UIElements .Parameter outputXaml If set, will output the Xaml for the data template rather than the object #> param( [Parameter(ValueFromPipeline=$true)] [Windows.UIElement] $control, [Parameter(Position=0)] [Hashtable]$binding, [switch]$outputXaml) process { $control | Get-ChildControl | ForEach-Object { if ($_.Resources) { foreach ($kv in @($_.Resources.GetEnumerator())) { $null = $_.Resources.Remove($kv.Key) } } } $xaml = [Windows.Markup.XamlWriter]::Save($control) $xml = [xml]$xaml if ($binding) { $binding.GetEnumerator() | ForEach-Object { $value = $_.Value if ($_.Key -like "*.*" ) { $chunks = $_.Key.Split(".") $targetName = $chunks[0] $bind = $chunks[1] $xml | Select-Xml "//*" | Where-Object { $_.Node.Name -eq $targetName } | ForEach-Object { $_.Node.SetAttribute($bind, "{Binding $value}") } } else { $property = $_.Key $value = $_.Value $xml | Select-Xml "." | Foreach-Object { @($_.Node.GetEnumerator())[0].SetAttribute($property, "{Binding $value}") } } } } $xaml = " $($xml.OuterXml) " if ($outputXaml) { $strWrite = New-Object IO.StringWriter [xml]$newXml = $xaml $newXml.Save($strWrite) return "$strWrite" } else { [Windows.Markup.XamlReader]::Parse($xaml) } }

Beschreibung

Converts the UIElement to a data template by stripping its resources, outputting the control as XAML, and enclosing within Template> tags

Links

Liste aller Commandlets Mehr über die Windows PowerShell

Bücher zur PowerShell

Buchcover Windows PowerShell 5.0 - Das Praxishandbuch (1. Auflage 2016/5. Auflage Gesamtreihe) Windows PowerShell 5.0 - Das Praxishandbuch (1. Auflage 2016/5. Auflage Gesamtreihe)
Autoren: Dr. Holger Schwichtenberg
erschienen 2016, 996 Seiten, 49,99 €
ISBN: 3446446435/978-3-446-44643-4
Bestellung: Amazon.de oder im Buchhandel über ISBN 3446446435/978-3-446-44643-4

Schulungen/Seminare

Offene Seminare zur PowerShell
In-House-Seminare zur PowerShell