gpt4 book ai didi

wpf - 在PowerShell中将WPF XAML链接到DataGrid

转载 作者:行者123 更新时间:2023-12-03 00:33:35 25 4
gpt4 key购买 nike

我一直跟随here指南,尝试使用PowerShell创建GUI,并且一切顺利,除了我的GUI中需要填充DataGrid之外。

在我的XML网格中,我有:

[xml]$form=@"
<Window
[...]
<Grid>
[...]
<DataGrid Name="CSVGrid" HorizontalAlignment="Left" Height="340" Margin="10,60,0,0" VerticalAlignment="Top" Width="765"/>
[...]
</Grid>
</Window>
"@

现在,在教程中使用以下内容来创建表单:
$XMLReader = (New-Object System.Xml.XmlNodeReader $Form)
$XMLForm = [Windows.Markup.XamlReader]::Load($XMLReader)

但是为了使我的DataGrid正常工作,我相信我需要在某个地方将“CSVGrid” DataGrid定义为“ system.Windows.Forms.DataGridView ”,但是我不确定如何将它们结合在一起。如果我尝试调用任何DataGrid属性(例如设置列数量或列名称)而没有定义它,则运行它会吐出错误。

有任何想法吗?

POSHGUI实现其表单的方式实际上可以很好地达到我的目的,但是我更喜欢在Visual Studio中编辑WPF表单。如果需要,我可以在POSHGUI中重建表单,但希望这里有一种方法可以将其 bundle 在一起,以便我可以继续使用VS GUI来编辑表单的GUI。

编辑:应该注意的是,如果不清楚,表单上不仅有数据网格。

编辑2:作为额外的信息,POSHGUI格式化控件的方式如下:
#the form itself
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '400,400'
$Form.text = "Form"
$Form.TopMost = $false
#a datagrid
$DataGridView1 = New-Object system.Windows.Forms.DataGridView
$DataGridView1.width = 382
$DataGridView1.height = 335
$DataGridView1.location = New-Object System.Drawing.Point(8,55)
#a button
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "My button"
$Button1.width = 126
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(156,13)
$Button1.Font = 'Microsoft Sans Serif,10'

然后将它们与以下内容联系在一起:
$Form.controls.AddRange(@($DataGridView1,$Button1))

因此,很高兴将DataGrid变量定义为“system.Windows.Forms.DataGridView”,&c,而将整个XML放入$ variable并将其传递到“System.Xml.XmlNodeReader”的方法并没有使我不相信这种区别,这就是为什么我无法调用任何DataGrid属性的原因。

但是,如果可以的话,我宁愿在Visual Studio中创建GUI。

编辑3:如果它完全有帮助,请检查intellisense下拉列表,可以找到各种DataGrid属性,但没有ColumnCount例如:

enter image description here

所以也许它按预期工作了?但是同时,如果像在POSHGUI示例中一样,将我的DataGrid变量显式定义为system.Windows.Forms.DataGridView,则设置ColumnCount会很顺利。

最佳答案

在深入探讨之前,您不能在WPF中使用 DataGridView (这是我将在此处显示的内容)。但是,您可以改为使用 DataGrid 来代替,这几乎是同一件事(在PowerShell中,比Windows Forms更短,更容易,这是POSHGUI所使用的(虽然是很棒的工具))。

如果您仍然感兴趣,这里是如何完成此操作的 super 基本演练。首先,定义XAML

$inputXML = @"
<Window x:Class="WpfApp2.MainWindow"
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:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid Name="Datagrid" AutoGenerateColumns="True" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="180" />
<DataGridTextColumn Header="Type" Binding="{Binding Type}" Width="233"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
"@

请注意,我们正在定义要显示的输入对象中的哪些列。尚未找到一种自动创建它们的好方法。

接下来,加载表单(使用 the method from my walkthrough on the topic here)
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
[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
#===========================================================================

$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}

现在,向此DataGridView添加一些项目。上面的代码在我们的 session 中还为我们提供了一个名为 $WPFGridView的变量。我们可以通过调用该变量的 .AddChild()方法并将其提供的属性至少与我们先前在 DataGrid中定义的属性相同的项添加到DataGridView中。
$WPFDatagrid.AddChild([pscustomobject]@{Name='Stephen';Type=123})
$WPFDatagrid.AddChild([pscustomobject]@{Name='Geralt';Type=234})

然后,为了显示我们的GUI,我们像这样调用 $FormShowDialog()方法,然后底部应向我们打招呼。
$Form.ShowDialog()

The image depicts a Windows 10 operating system displaying a Windows Presentation Foundation User Interface which contains a datagrid with two columns that contain the items added in the code snippet above

完整的代码示例为 available here。如果您想了解有关使用WPF GUI元素的更多信息,我有一个 full walkthrough available here

关于wpf - 在PowerShell中将WPF XAML链接到DataGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52405852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com