如何使用 PowerShell、WPF 和 XAML 从 DataGrid 中删除选定的行?

问题描述 投票:0回答:1

你好,

我对 WPFPowerShell 有点陌生。我的实际要求是在单击按钮时从 DataGrid 中删除选定的行。因此,单击按钮后,选定的行应该从 DataGrid GUI 中消失。

这可能有点复杂,因为我使用了这些技术:PowerShell、XAML、WPF 和 SCCM(从中获取一些数据)。

这是 XAML 文件的以下代码

<Grid>

<DataGrid Name="DGDriverPackage"
                Margin="10"
                MinWidth="200"
                HorizontalAlignment="Stretch" 
                VerticalAlignment="Stretch"
                Height="480"
                CanUserReorderColumns="True" 
                CanUserResizeColumns="True"
                GridLinesVisibility="None"
                FontFamily="arial"
                FontSize="16"
                FrozenColumnCount="2"
                FontWeight="Regular"
                SelectionUnit="FullRow"
                Cursor="Hand"
                IsReadOnly="True"
                CanUserAddRows="False"
                ScrollViewer.VerticalScrollBarVisibility="Visible"
                ScrollViewer.HorizontalScrollBarVisibility="Auto"
                Visibility="Hidden">
                    
                <DataGrid.Columns>
                    <DataGridCheckBoxColumn ElementStyle="{DynamicResource MetroDataGridCheckBox}" 
                        EditingElementStyle="{DynamicResource MetroDataGridCheckBox}" 
                        Header="Select" 
                        Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=IsSelected, Mode=OneWay}"/>
                </DataGrid.Columns>
</DataGrid>

<Button Name="BtnDelete"
        Style="{DynamicResource MahApps.Styles.Button.Square}"
        Content="Delete"
        HorizontalAlignment="Right" 
        Margin="10"
        VerticalAlignment="Top"
        Width="150"
        Height="30"
        Visibility="Hidden"/>
</Grid>

这是 PowerShellcode,我将一些 data 设置到 DataGrid 中。

$DGV_PFM_OSD = $PFMOSDCleanupXaml.FindName("DGDriverPackage")
$SCCMApplications =  @()
foreach($App in $vUniqueApplicationofOldpfm){
       $SCCMApplications  += Get-CMApplication -Fast -Name $App | Select-Object LocalizedDisplayName, IsDeployed, DateCreated
}
$Fields = @('LocalizedDisplayName', "IsDeployed", "DateCreated")
$Datatable = New-Object System.Data.DataTable
[void]$Datatable.Columns.AddRange($Fields)
foreach ($SCCMApp in $SCCMApplications)
    {
         $Array = @()
         Foreach ($Field in $Fields)
         {
              $array += $SCCMApp.$Field
         }
         [void]$Datatable.Rows.Add($array)
}
$DGV_PFM_OSD.ItemsSource  = $Datatable.DefaultView
$Global:isDelete = 1

这是我编写的PowerShell代码,用于从DataGridForm.ps1文件删除行

$BtnDelete = $PFMOSDCleanupXaml.FindName("BtnDelete") $BtnDelete.add_Click({ switch( $Global:isDelete ) { # Deleting selected applications. 1 { foreach($SelectedApp in $DGV_PFM_OSD.SelectedItems){ $DGV_PFM_OSD.Items.RemoveAt($SelectedApp.Index) //This line is not working. $DGV_PFM_OSD.Items.Remove($SelectedApp) //This line is not working. } } })
下面是我的 DataGrid 的样子

有人可以建议我缺少什么或我在这里做错了什么吗?

wpf powershell xaml datagrid
1个回答
0
投票
您无法修改正在枚举的集合(例如通过 foreach),因为这会使当前的

IEnumerator

 无效。

最简单的方法是使用

DataGrid.DeleteCommand

,因为这不需要任何索引处理等。
使用 
DataGrid
 实例作为命令目标来调用命令将触发 DataGrid 删除所有选定的项目。

$BtnDelete.add_Click({ switch($Global:isDelete) { # Deleting selected applications. 1 { [System.Windows.Controls.DataGrid]::DeleteCommand($null, $DGV_PFM_OSD) } } })
    
© www.soinside.com 2019 - 2024. All rights reserved.