无法编辑我的 DataGrid WPF Framework 4.5 的单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13385950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot edit cells of my DataGrid WPF Framework 4.5
提问by jss
My ObservableCollection has three items, and the rows are consistently shown in the datagrid. I cannot turn into edit mode a single cell of my DataGrid. I tried click, click-click, double-click, F2,…, but the whole row stay selected. How can I let the user edit the datagrid. I found similar datagrid edit-questions in other posts but no-one solved my problem. Here is the code (WPF NetFramework 4.5). Pursposely, only the first column is non-editable (readonly).
我的 ObservableCollection 有三个项目,并且行在数据网格中一致显示。我无法将 DataGrid 的单个单元格转为编辑模式。我尝试单击、单击、双击、F2、...,但整行保持选中状态。如何让用户编辑数据网格。我在其他帖子中发现了类似的数据网格编辑问题,但没有人解决我的问题。这是代码(WPF NetFramework 4.5)。目的是,只有第一列是不可编辑的(只读)。
<DataGrid Name="myDataGrid" Grid.Row="2" AutoGenerateColumns="False" ItemsSource="{Binding}" IsReadOnly="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="name" IsReadOnly="True" Width="200" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Formulation" Width="100" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding FormulationStr}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBlock Text="{Binding FormulationStr}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="volume Diff" Width="100" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding volumeDiff}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBlock Text="{Binding volumeDiff}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
回答by Rohit Vats
You have placed TextBlockin cell templateas well as in cell editing template. That's why you are not noticing any change on pressing F2 and double-clicking the cell since no matter what it will always be TextBlockwhich you can't edit.
你已经把TextBlock在cell template和中cell editing template。这就是为什么您在按 F2 并双击单元格时没有注意到任何变化的原因,因为无论它是什么TextBlock,您都无法编辑。
Either placed TextBoxin your CellEditingTemplatelike this -
要么放在TextBox你CellEditingTemplate这样的——
<DataGridTemplateColumn Header="Formulation" Width="100" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding FormulationStr}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding FormulationStr}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
Or either simply use the DataGridTextColumnin place of DataGridTemplateColumnwhich internally provides the support what are you trying to achive by the above code -
或者简单地使用DataGridTextColumnin 代替DataGridTemplateColumn它在内部提供支持你试图通过上述代码实现的目标 -
<DataGridTextColumn Header="Formulation" Width="100" IsReadOnly="False" Binding="{Binding FormulationStr}" />

