【问题标题】:How to enable cell editing for Datagrid in Powershell?如何在 Powershell 中为 Datagrid 启用单元格编辑?
【发布时间】:2020-05-06 16:46:51
【问题描述】:

我正在寻找如何在 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

【问题讨论】:

    标签: wpf powershell xaml datagrid powershell-3.0


    【解决方案1】:

    为了在 Datagrid 中获得 Edit 体验,您必须使用该方法为 Grid 指定 .ItemsSource。这是可行的,因为这将实现该体验所需的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})
    

    这是它的外观:

    您可以阅读更多关于为什么需要 herehere 的信息。

    【讨论】:

    • 嗨,非常感谢。你解决了我的问题。我没有意识到这一点。我一直在尝试通过 AddChild 方法添加。
    猜你喜欢
    • 2012-12-04
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多