I found this question about loading .net 4.0 dll in Powershell.
Now I want to know which Add-Type I have to use, to be able to use t开发者_高级运维he WPF Datagrid from PowerShell ISE
before the following works
[xml] $xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<DataGrid Height="200" Width="500" HorizontalAlignment="Left" Margin="12,21,0,0"
Name="McDataGrid" VerticalAlignment="Top" RowHeight="30" ColumnWidth="100" >
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Form=[Windows.Markup.XamlReader]::Load( $reader )
$Form.ShowDialog()
It was a combination of multiple errors.
- A slash was missing at the end of the tag
- I had a TYPO inpowershell_ise.exe.Config and the .Net 4.0 assemblies didn't load
It's a good to check which assemblies are loaded with
[System.AppDomain]::CurrentDomain.GetAssemblies() | sort location
Now here is a working solution
function Invoke-sql1
{
param( [string]$sql,
[System.Data.SQLClient.SQLConnection]$connection
)
$cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection)
$ds = New-Object system.Data.DataSet
$da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
$da.fill($ds) | Out-Null
return $ds.tables[0].rows
}
[xml] $xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid
AutoGenerateColumns="True"
HorizontalAlignment="Left"
Name="dataGrid1"
VerticalAlignment="Top"
Width="330"
HeadersVisibility="All"
>
<DataGrid.Columns>
<DataGridTextColumn Header="title"
Binding="{Binding title}"
/>
<DataGridTextColumn Header="itemid"
Binding="{Binding itemid}"
/>
</DataGrid.Columns>
</DataGrid >
</Grid>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Form=[Windows.Markup.XamlReader]::Load( $reader )
$con = New-Object System.Data.SqlClient.SqlConnection
$con.ConnectionString = "Data Source=localhost;Initial Catalog=ABDATA;Integrated Security=True"
$con.open()
$sql = @"
SELECT 'abc' title, 3 itemid
union
SELECT 'xyz' title, 2 itemid
union
SELECT 'efg' title, 1 itemid
"@
$dg = $Form.FindName("dataGrid1")
$dg.ItemsSource = @(Invoke-sql1 $sql $con)
$Form.ShowDialog()
The only monor problem is, that I have to define the columns myself. I thought that could be done automatically.
精彩评论