在PowerShell中使用XAML / WPF,如何填充列表框?

时间:2022-06-01 20:33:58

I've found a number of great threads here but can't seem to track down my answer. I'm making a GUI using Visual Studio and copy/pasting the XAML into PowerShell. I know I should be doing this in c#, but as my knowledge isn't there yet, it's pure PowerShell for me.

我在这里找到了许多很棒的主题,但似乎无法追查我的答案。我正在使用Visual Studio制作GUI并将XAML复制/粘贴到PowerShell中。我知道我应该在c#中这样做,但是因为我的知识还没有,所以对我来说它是纯粹的PowerShell。

So I've got my GUI made, but I can't seem to populate my data fields. Doing other things like textboxes were solvable, but I can't seem to get this listview / data grid to populate with values.

所以我已经制作了我的GUI,但我似乎无法填充我的数据字段。做其他事情,如文本框是可以解决的,但我似乎无法让这个listview /数据网格填充值。

在PowerShell中使用XAML / WPF,如何填充列表框?

At this moment, the connection to Azure has been removed, until I can resolve this hitch of adding items to my list box.

此时,已删除与Azure的连接,直到我可以解决将此项添加到列表框的问题。

XAML to draw my form

XAML绘制我的表单

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = @'
<Window 
        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:Azure"
        Title="Azure Accelerator" Height="300" Width="315">
    <Grid Margin="0,0,174,0">
        <Image Name="image" HorizontalAlignment="Left" Height="46" Margin="10,10,-97,0" VerticalAlignment="Top" Width="210" Source="C:\Users\stephen\Dropbox\My Code\Powershell\WPF\mslogo.png"/>
        <TextBlock Name="textBlock" HorizontalAlignment="Left" Height="21" Margin="10,61,-140,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="248" Text="VM PickerUse"/>
        <Button Name="btnOK" Content="OK" HorizontalAlignment="Left" Margin="217,268,-160,0" VerticalAlignment="Top" Width="75" Height="23"/>
        <Button Name="btnExit" Content="Cancel" HorizontalAlignment="Left" Margin="12,268,0,0" VerticalAlignment="Top" Width="75" Height="23"/>
        <ListView Name="listView" HorizontalAlignment="Left" Height="108" Margin="12,107,-140,0" VerticalAlignment="Top" Width="246">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="VMName" DisplayMemberBinding ="{Binding VMName}"/>
                    <GridViewColumn Header="Status" DisplayMemberBinding ="{Binding Status}"/>
                    <GridViewColumn Header="Other"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>
'@

Loading the XAML into memory/making objects

将XAML加载到内存/制作对象中

#Read XAML

$reader=(New-Object System.Xml.XmlNodeReader $xaml) 
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."}

#===========================================================================
# Store Form Objects In PowerShell
#===========================================================================

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

Where I probably need help

#Try to setup a dummy entry
$vmpicklistView.items.Add( @{'VMName'='1';Status="AccessDenied";'Other'='1'})

#===========================================================================
# Shows the form
#===========================================================================
$Form.ShowDialog() | out-null

As you can see from my screen shot, when I added a binding to the columns (which I thought would instantiate the columns and let me plug in values for them...nope) they no longer update when I try to add a new item. However, the 'Other' column, which I did not apply the binding to, does at least show someput, but it incorrectly lists Collection, as if it is trying to display the whole hashtable.

正如你从我的屏幕截图中看到的那样,当我添加一个绑定到列(我认为会实例化列并允许我为它们插入值... nope)时,当我尝试添加新项目时它们不再更新。但是,我没有应用绑定的“其他”列至少显示了一些输出,但它错误地列出了Collection,就像它试图显示整个哈希表一样。

So, my final question, how do I add items to a listview?

所以,我的最后一个问题是,如何将项目添加到列表视图?

3 个解决方案

#1


7  

Alright, I figured out the answer. It turns out that when I was using .Add, I should have been specifying a PowerShell custom object as my overload, not a simple hashtable as I was doing before. When I changed my code to the following:

好吧,我想出了答案。事实证明,当我使用.Add时,我应该将PowerShell自定义对象指定为我的重载,而不是像以前那样简单的哈希表。当我将代码更改为以下内容时:

#Add DisplayMemberBindings for all columns
<GridViewColumn Header="VMName" DisplayMemberBinding ="{Binding VMName}"/>
<GridViewColumn Header="Status" DisplayMemberBinding ="{Binding Status}"/>
<GridViewColumn Header="Other" DisplayMemberBinding ="{Binding Other}"/>

And then modify my Add statement as well:

然后修改我的Add语句:

$vmpicklistView.items.Add([pscustomobject]@{'VMName'='1';Status="Access Denied";Other="Yes"})

I'm able to populate my fields, like so

我能够填充我的田地,就像这样

#Make Dummy Entries 
1..15 | % {
if ($_ % 2){$vmpicklistView.items.Add([pscustomobject]@{'VMName'="VM_$($_)";Status="Online";Other="Yes"})}
else{$vmpicklistView.items.Add([pscustomobject]@{'VMName'="VM_$($_)";Status="Access Denied";Other="Yes"})}
}

在PowerShell中使用XAML / WPF,如何填充列表框?

Why did I have to do this?

我为什么要这样做?

Here is my interpretation as to why this was needed. PowerShell Custom Objects provide an Object with Named values, which I can pluck out using bindings, while a hashtable is a collection of Key/Value pairs, not well suited for this purpose.

以下是我为何需要这样做的解释。 PowerShell自定义对象提供了一个具有命名值的对象,我可以使用绑定来提取,而哈希表是键/值对的集合,不太适合此目的。

I hope this answer has helped anyone else who got stumped as I did!

我希望这个答案能帮助那些像我一样难倒的人!

#2


2  

WPF is generally used to bind to viewmodel object properties not dictionary or hashtable entries. There is a syntax to bind to dictionary entries which would make the GridViewColumn bindings look like this:

WPF通常用于绑定到viewmodel对象属性,而不是字典或哈希表条目。有一种语法可以绑定到字典条目,这将使GridViewColumn绑定看起来像这样:

<GridViewColumn Header="VMName" DisplayMemberBinding ="{Binding [VMName]}"/>
<GridViewColumn Header="Status" DisplayMemberBinding ="{Binding [Status]}"/>

Note the use of square brackets to indicate that the value comes from an indexer.

请注意使用方括号表示值来自索引器。

As for why the "Other" column appears as "(Collection)", I believe the default binding is just going to display the object and WPF must render dictionaries and hashtables as "(Collection)"

至于为什么“其他”列显示为“(Collection)”,我相信默认绑定只是显示对象而WPF必须将字典和哈希表呈现为“(Collection)”

Try adding a similar binding for Other:

尝试为其他人添加类似的绑定:

<GridViewColumn Header="Other" DisplayMemberBinding ="{Binding [Other]}"/>

#3


1  

There is a much simpler way of doing this.

有一种更简单的方法可以做到这一点。

Say you have a setup similar to the question above

假设您有类似于上述问题的设置

<ListView Name="Collections">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="Collection Name" DisplayMemberBinding="{Binding CollectionName}" />
      <GridViewColumn Header="Collection Count" DisplayMemberBinding="{Binding CollectionCount}" />
    </GridView>
  </ListView.View>
</ListView>

and in Powershell you are getting say the list of objects from some provider (in this case ConfigurationManager)

在Powershell中,您可以从一些提供程序中获取对象列表(在本例中为ConfigurationManager)

$DCs = Get-CMDeviceCollection | ? { $_.CollectionRules.RuleName -contains "OU Query" } 

Now this out of the box has properties that do not align with my DisplayMemberBindings so we have to make a collection of objects that do so

现在这个开箱即用的属性与我的DisplayMemberBindings不对齐所以我们必须制作一个这样做的对象的集合

$BindableDCs = $DCs | Select-Object -Property @{Name='CollectionName';Expression={$_.Name}}, @{Name='CollectionCount';Expression={$_.MemberCount}}

Now (assuming the code to load the XAML and discover the named elements and put them in powershell variables has been run)

现在(假设已经运行了加载XAML并发现命名元素并将它们放入powershell变量的代码)

$Collections.ItemsSource = $BindableDCs

Done.

完成。

Or if you want to do it the one liner powershell way

或者,如果你想做一个内衬powershell方式

$Collections.ItemsSource = Get-CMDeviceCollection | ? { $_.CollectionRules.RuleName -contains "OU Query" } | Select-Object -Property @{Name='CollectionName';Expression={$_.Name}}, @{Name='CollectionCount';Expression={$_.MemberCount}}

#1


7  

Alright, I figured out the answer. It turns out that when I was using .Add, I should have been specifying a PowerShell custom object as my overload, not a simple hashtable as I was doing before. When I changed my code to the following:

好吧,我想出了答案。事实证明,当我使用.Add时,我应该将PowerShell自定义对象指定为我的重载,而不是像以前那样简单的哈希表。当我将代码更改为以下内容时:

#Add DisplayMemberBindings for all columns
<GridViewColumn Header="VMName" DisplayMemberBinding ="{Binding VMName}"/>
<GridViewColumn Header="Status" DisplayMemberBinding ="{Binding Status}"/>
<GridViewColumn Header="Other" DisplayMemberBinding ="{Binding Other}"/>

And then modify my Add statement as well:

然后修改我的Add语句:

$vmpicklistView.items.Add([pscustomobject]@{'VMName'='1';Status="Access Denied";Other="Yes"})

I'm able to populate my fields, like so

我能够填充我的田地,就像这样

#Make Dummy Entries 
1..15 | % {
if ($_ % 2){$vmpicklistView.items.Add([pscustomobject]@{'VMName'="VM_$($_)";Status="Online";Other="Yes"})}
else{$vmpicklistView.items.Add([pscustomobject]@{'VMName'="VM_$($_)";Status="Access Denied";Other="Yes"})}
}

在PowerShell中使用XAML / WPF,如何填充列表框?

Why did I have to do this?

我为什么要这样做?

Here is my interpretation as to why this was needed. PowerShell Custom Objects provide an Object with Named values, which I can pluck out using bindings, while a hashtable is a collection of Key/Value pairs, not well suited for this purpose.

以下是我为何需要这样做的解释。 PowerShell自定义对象提供了一个具有命名值的对象,我可以使用绑定来提取,而哈希表是键/值对的集合,不太适合此目的。

I hope this answer has helped anyone else who got stumped as I did!

我希望这个答案能帮助那些像我一样难倒的人!

#2


2  

WPF is generally used to bind to viewmodel object properties not dictionary or hashtable entries. There is a syntax to bind to dictionary entries which would make the GridViewColumn bindings look like this:

WPF通常用于绑定到viewmodel对象属性,而不是字典或哈希表条目。有一种语法可以绑定到字典条目,这将使GridViewColumn绑定看起来像这样:

<GridViewColumn Header="VMName" DisplayMemberBinding ="{Binding [VMName]}"/>
<GridViewColumn Header="Status" DisplayMemberBinding ="{Binding [Status]}"/>

Note the use of square brackets to indicate that the value comes from an indexer.

请注意使用方括号表示值来自索引器。

As for why the "Other" column appears as "(Collection)", I believe the default binding is just going to display the object and WPF must render dictionaries and hashtables as "(Collection)"

至于为什么“其他”列显示为“(Collection)”,我相信默认绑定只是显示对象而WPF必须将字典和哈希表呈现为“(Collection)”

Try adding a similar binding for Other:

尝试为其他人添加类似的绑定:

<GridViewColumn Header="Other" DisplayMemberBinding ="{Binding [Other]}"/>

#3


1  

There is a much simpler way of doing this.

有一种更简单的方法可以做到这一点。

Say you have a setup similar to the question above

假设您有类似于上述问题的设置

<ListView Name="Collections">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="Collection Name" DisplayMemberBinding="{Binding CollectionName}" />
      <GridViewColumn Header="Collection Count" DisplayMemberBinding="{Binding CollectionCount}" />
    </GridView>
  </ListView.View>
</ListView>

and in Powershell you are getting say the list of objects from some provider (in this case ConfigurationManager)

在Powershell中,您可以从一些提供程序中获取对象列表(在本例中为ConfigurationManager)

$DCs = Get-CMDeviceCollection | ? { $_.CollectionRules.RuleName -contains "OU Query" } 

Now this out of the box has properties that do not align with my DisplayMemberBindings so we have to make a collection of objects that do so

现在这个开箱即用的属性与我的DisplayMemberBindings不对齐所以我们必须制作一个这样做的对象的集合

$BindableDCs = $DCs | Select-Object -Property @{Name='CollectionName';Expression={$_.Name}}, @{Name='CollectionCount';Expression={$_.MemberCount}}

Now (assuming the code to load the XAML and discover the named elements and put them in powershell variables has been run)

现在(假设已经运行了加载XAML并发现命名元素并将它们放入powershell变量的代码)

$Collections.ItemsSource = $BindableDCs

Done.

完成。

Or if you want to do it the one liner powershell way

或者,如果你想做一个内衬powershell方式

$Collections.ItemsSource = Get-CMDeviceCollection | ? { $_.CollectionRules.RuleName -contains "OU Query" } | Select-Object -Property @{Name='CollectionName';Expression={$_.Name}}, @{Name='CollectionCount';Expression={$_.MemberCount}}