首页 > 解决方案 > 如何在 Powershell 中为 Datagrid 启用单元格编辑?

问题描述

我正在寻找解决方案,如何在双击 wpf 数据网格视图中的项目时启用单元格编辑和添加新单元格。

我实际上无法追踪我在哪里犯了错误。每当我双击该项目时,它都会引发错误,因为“不允许编辑”并且 Windows 窗体会自动关闭。

问题:您能否帮助以下代码如何在鼠标双击事件上启用对 WPF 数据网格的编辑?

[xml]$XAML = @'
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Server Rebuild Request" Height="600" Width="1000" Background="White" ResizeMode="NoResize">
    <Grid Margin="0,0,0,0" Background="White">

    <Grid
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">


    <Grid>
   <DataGrid Name="Datagrid" IsReadOnly="False" CanUserAddRows="True" CanUserDeleteRows="True" CanUserReorderColumns="True" CanUserSortColumns="True" AutoGenerateColumns="False" RowBackground="#D1F29C" RowDetailsVisibilityMode="Visible" Cursor="Hand"  >


        <DataGrid.Columns >


           <DataGridTextColumn Header="TaskName" IsReadOnly="False" Binding="{Binding TaskName}" Width="100" Foreground="#FF631F1F" />
           <DataGridTextColumn Header="Category" IsReadOnly="False" Binding="{Binding Category}" Width="100" Foreground="#FF631F1F" />


            <DataGridTextColumn Header="Status" Binding="{Binding Status}" Width="80" Foreground="#FF631F1F"/>
            <DataGridTextColumn Header="Created By" Binding="{Binding 'Created By'}" Width="100" Foreground="#FF631F1F"/>
            <DataGridTextColumn Header="Asigned To" Binding="{Binding 'Asigned To'}" Width="100" Foreground="#FF631F1F"/>
            <DataGridTextColumn Header="Dead Line" Binding="{Binding 'Dead Line'}" Width="90" Foreground="#FF631F1F"/>
            <DataGridTextColumn Header="Age" Binding="{Binding 'Age'}" Width="50" Foreground="#FF631F1F"/>
            <DataGridHyperlinkColumn Header="Source URL" Binding="{Binding 'Source URL'}" Width="300" FocusManager.IsFocusScope="True" />
            <DataGridTemplateColumn Header="Action">


                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <ComboBox 
                                    Height="22" >

                                    <ComboBoxItem AllowDrop="True" Content="'In Progress','Done'" IsSelected="False"/>
                                </ComboBox>
                            </DataTemplate>
                            </DataGridTemplateColumn.CellEditingTemplate>

                    </DataGridTemplateColumn>



                    <DataGridTemplateColumn>

                        <DataGridTemplateColumn.CellTemplate>


                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>


        </DataGrid.Columns>



    </DataGrid>
    </Grid>



</Grid>


    </Grid>
</Window>
'@


#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."; exit}

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

$Datagrid.RowStyle=[System.Windows.Style]::new()
$Datagrid.IsEnabled=$true
#$Datagrid.is
$DataGrid.AddChild([pscustomobject]@{TaskName='Test1';Category='CTB';Status='InProgress'; 'Created By'='TestCode'; 'Asigned To'='TestCode'; 'Dead Line'=$DeadLine; Age=$Age; 'Source URL'=$href})
$DataGrid.AddChild([pscustomobject]@{TaskName='Test2';Category='CTB';Status='InProgress'; 'Created By'='TestCode'; 'Asigned To'='TestCode'; 'Dead Line'=$DeadLine; Age=$Age; 'Source URL'=$href})
$DataGrid.AddChild([pscustomobject]@{TaskName=$null;Category=$null;Status=$null; 'Created By'=$null; 'Asigned To'=$null; 'Dead Line'=$DeadLine; Age=$Age; 'Source URL'=$href})



$Datagrid.Add_Mousedoubleclick({
Write-Host "Mouse double clicked"
$Datagrid.Items.EditItem($Datagrid.SelectedItem)
})


$Form.ShowDialog()

双击项目时出现的错误。

使用“0”参数调用“ShowDialog”的异常:“此视图不允许使用'EditItem'。” 在 C:\Users\bbiswal041119\Downloads\RDS_News_Letter\TestDataGrid.ps1:97 char:1 + $Form.ShowDialog() + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified : (:) [], MethodInvocationException + FullyQualifiedErrorId : InvalidOperationException

标签: wpfpowershellxamldatagridpowershell-3.0

解决方案


为了Edit在 Datagrid 中获得体验,您必须.ItemsSource使用该方法为 Grid 指定。这是可行的,因为这将实现该IEditable体验所需的接口。

请注意,我注释掉了您的$DataGrid.AddChild台词。

$itemsArray = @([pscustomobject]@{TaskName='Test1';Category='CTB';Status='InProgress'; 'Created By'='TestCode'; 'Asigned To'='TestCode'; 'Dead Line'=$DeadLine; Age=$Age; 'Source URL'=$href},
    [pscustomobject]@{TaskName='Test2';Category='CTB';Status='InProgress'; 'Created By'='TestCode'; 'Asigned To'='TestCode'; 'Dead Line'=$DeadLine; Age=$Age; 'Source URL'=$href},
    [pscustomobject]@{TaskName=$null;Category=$null;Status=$null; 'Created By'=$null; 'Asigned To'=$null; 'Dead Line'=$DeadLine; Age=$Age; 'Source URL'=$href}
)
$Datagrid.ItemsSource = $itemsArray
#$DataGrid.AddChild([pscustomobject]@{TaskName='Test1';Category='CTB';Status='InProgress'; 'Created By'='TestCode'; 'Asigned To'='TestCode'; 'Dead Line'=$DeadLine; Age=$Age; 'Source URL'=$href})
#$DataGrid.AddChild([pscustomobject]@{TaskName='Test2';Category='CTB';Status='InProgress'; 'Created By'='TestCode'; 'Asigned To'='TestCode'; 'Dead Line'=$DeadLine; Age=$Age; 'Source URL'=$href})
#$DataGrid.AddChild([pscustomobject]@{TaskName=$null;Category=$null;Status=$null; 'Created By'=$null; 'Asigned To'=$null; 'Dead Line'=$DeadLine; Age=$Age; 'Source URL'=$href})

这是它的外观: 在此处输入图像描述

您可以在此处此处阅读有关为什么需要此功能的更多信息。


推荐阅读