wpf - 如何在 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
解决方案
为了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})
推荐阅读
- javascript - 如何使用 Angular 材料日期时间选择器格式化日期
- java - 将一个 char 数组分成三份
- flutter - 重复通知 - Flutter
- azure - 获取ARM模板中App配置的只读连接字符串
- java - GridBagConstraints Java中gridwidth中的最大单元格数
- python - Python保存颜色选择器
- javascript - 仍然未定义到带有反应项目的节点中的属性
- oracle - 为什么我们使用“/”来标记 MySQL 查询的结束?
- r - 如何将 R 中生成的数据透视表中的每个值除以总计?
- javascript - 在小吃博览会中单击停止按钮时如何停止声音?