【发布时间】:2019-11-16 08:29:49
【问题描述】:
所以也许我以错误的方式接近这个问题,请随时提供任何建议或提示。 目前正在使用 powershell 处理 wpf 中的 DataGrid 表来执行所有逻辑。这是表格:
从选定行正确提取所有数据(包括组合框选定值)的最佳方法是什么? 目前我正在尝试这个:
$test = $AddServerwpf.ServerGrid.SelectedItems[0]
Write-Host "Selected: $test"
对于上下文 $AddServerwpf 包含该窗口的所有对象。 ServerGrid 是DataGrid 对象。我正在使用 .SelectedItems[0] 来获取该行。
这会返回:
Selected: @{Server=server1; Environment=Prod; ServiceAccount=System.Object[]}
如果我更进一步:
$AddServerwpf.ServerGrid.SelectedItems[0].ServiceAccount
我明白了:
Selected: account1 account2 account3
这显然不能告诉我从下拉列表中选择了哪个帐户。如何获得组合框选择?我已经通过谷歌深入研究了有关此问题的其他问题,但我还没有找到有效的答案。如何正确地将组合框绑定到数据网格?还是以某种方式获取 Combobox 对象并提取文本更好?
这是 XAML:
<Window x:Class="ServerManagmentApp.AddServer" x:Name="AddServerWindow"
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:ServerManagmentApp"
mc:Ignorable="d"
Title="AddServer" Height="359.7" Width="387.4" Background="#FF2B2929">
<Grid>
<Button x:Name="AddServerButton" Content="Add Server" HorizontalAlignment="Left" Margin="220,260,0,0" VerticalAlignment="Top" Width="120" Height="40" Background="#FF1FD14F"/>
<DataGrid x:Name="ServerGrid" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="213" Width="275" Background="#FF888F8A" SelectionMode="Single">
<DataGrid.Columns>
<DataGridTextColumn Header="Server" Binding="{Binding Server}" Width="*" />
<DataGridTextColumn Header="Environment" Binding="{Binding Environment}" Width="*" />
<DataGridTemplateColumn Header="ServiceAccount" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="ServiceAccount" ItemsSource="{Binding ServiceAccount}" SelectedIndex="0"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
花了几个小时试图克服这个障碍,我们将不胜感激。我对此并不陌生,并试图在使用 powershell 时了解此绑定的工作原理。
编辑: 如果有帮助,这就是我填充 DataGrid 的方式
$list = "account1","account2","account3"
$AddServerwpf.ServerGrid.AddChild([pscustomobject{Server='server1';Environment='Prod';ServiceAccount=$list})
$AddServerwpf.ServerGrid.AddChild([pscustomobject]@{Server='server2';Environment='Prod';ServiceAccount=$list})
$AddServerwpf.ServerGrid.AddChild([pscustomobject]@{Server='server3';Environment='Prod';ServiceAccount=$list})
【问题讨论】:
标签: wpf powershell combobox binding datagrid