Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2314

Formular WPF Combox Bind XML Data mit Powershell

$
0
0

Hallo,

mein Problem. Ich möchte in einem WPF Formular eine Combobox mit Daten aus einer XML Datei befüllen. Was ich nicht machen möchte jeden einzelnen Wert hinzufügen mittels items.AddNewItem("Wert").

Sondern ich möchte die Daten an das die Combobox binden. In meinem Beispiel möchte ich in der Combobox alle Anteiölungen der Firma "Company A" haben.

Über .SlectNodes("//Firmen/Firma[@Name='Company A']/Abteilungen/Abteilung") kann ich die entsprechenden Abteilungen auswählen. Über ItemsSource versuche ich nun die Daten an die Combobox zu binden. Es klappt bedingt. Es werden drei Werte in der Combobox hinzugefügt, sind aber nicht sichtbar. Ich vermute, dass ich hier den Verweis auf den Namen noch mitgeben muss. Oder mache ich alles falsch! 

Auf jeden Fall würde ich mich über jede Anregung, Hilfe und Lösung freuen.

Gruß

Christian

Hier noch mein Beispiel Code:

function Load-XamlForm
{
	param
	(
		[Parameter(Mandatory = $true,
				   HelpMessage = 'A string with the Xaml Form')]
		[string]$inputXML
	)
	Add-Type -AssemblyName PresentationFramework
	$inputXML = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^<Win.*', '<Window' -replace "x:Bind", "Binding"
	[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
	[xml]$XAML = $inputXML
	#===========================================================================
	# Read XAML
	#===========================================================================
	$reader = (New-Object System.Xml.XmlNodeReader $XAML)
	try { $Form = [Windows.Markup.XamlReader]::Load($reader) }
	catch [System.Management.Automation.MethodInvocationException] {
		Write-Warning "We ran into a problem with the XAML code.  Check the syntax for this control..."
		write-host $error[0].Exception.Message -ForegroundColor Red
		if ($error[0].Exception.Message -like "*button*")
		{
			write-warning "Ensure your &lt;button in the `$inputXML does NOT have a Click=ButtonClick property.  PS can't handle this`n`n`n`n"
		}
	}
	catch
	{
		#===========================================================================
		# if it broke some other way 
		# <span class="wp-smiley wp-emoji wp-emoji-bigsmile" title=":D">:D</span>
		#===========================================================================
		Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."
	}
	#===========================================================================
	# Store Form Objects In PowerShell
	#===========================================================================
	$Elements = @{ }
	$xaml.SelectNodes("//*[@Name]") | %{ $Elements[$_.Name] = $Form.FindName($_.Name) }
	return $Form, $Elements
}


#===========================================================================
# The Form as xaml
#===========================================================================

$FormXML = '<Window x:Class="TestComboxBoxChange"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Neuer_Domänenbenutzer"
        mc:Ignorable="d"
        Title="TestComboxBoxChange" Height="450" Width="800"><Grid Margin="0,0,427,326"><ComboBox x:Name="ComboBoxTest" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="316"/><TextBox x:Name="TextBoxTest" HorizontalAlignment="Left" Margin="10,37,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="316"/></Grid></Window>'

$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
if (($ScriptPath.Substring($ScriptPath.Length - 1, 1)) -ne "\"){$ScriptPath += "\"}
cls
$Form, $Elements = Load-XamlForm $FormXML
$ConfigFile = $ScriptPath + 'example.xml'
$objConfig = New-Object System.XML.XMLDocument
$objConfig.Load($ConfigFile)
$SelectNode = "//Firmen/Firma[@Name='Company A']/Abteilungen/Abteilung"
$xmlAbteilungen = $objConfig.SelectNodes($SelectNode)
$Elements.ComboBoxTest.ItemsSource = $xmlAbteilungen
$TextBoxTest = $Elements.TextBoxTest
$Form.ShowDialog()
$xmlAbteilungen

$Elements.ComboBoxTest.items.AddNewItem()

Die XML Datei:

<?xml version="1.0"?><!--Firmen Liste--><Firmen><Firma Name="Company A" Default="True" Webseite="https://www.companya.de"><Standorte><Standort Name="Zentrale" Straße="Zentralstrasse 123" Postleitzahl="12345" Ort="Dorfhausen" Telefon="" Fax="" /><Standort Name="Lager" Straße="Lagerstrasse 12" Postleitzahl="23456" Ort="Hausedorf" Telefon="" Fax="" /></Standorte><Abteilungen><Abteilung Name="Einkauf" OU="111" /><Abteilung Name="Fachhandel" OU="222" /><Abteilung Name="IT" OU="333" /></Abteilungen></Firma><Firma Name="Company B" Default="False" Webseite="https://www.companyb.de"><Standorte><Standort Name="Zentrale" Straße="Weinstraße 2" Postleitzahl="78901" Ort="KeinAhnung" Telefon="" Fax="" /></Standorte><Abteilungen /></Firma></Firmen>



Viewing all articles
Browse latest Browse all 2314


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>