无法编辑我的DataGrid WPF Framework 4.5的单元格 [英] Cannot edit cells of my DataGrid WPF Framework 4.5
问题描述
我的ObservableCollection有三个项目,这些行一直显示在datagrid中。
我不能将DataGrid的单个单元格变成编辑模式。我尝试点击,点击,双击,F2,...,但整行保持选择。我如何让用户编辑datagrid。
我在其他帖子中发现了类似的datagrid编辑问题,但没有人解决我的问题。这是代码(WPF NetFramework 4.5)。通常,只有第一列是不可编辑的(只读)。
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).
<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>
推荐答案
您已将 TextBlock
在单元格模板
以及单元格编辑模板
中。这就是为什么你没有注意到按F2和双击单元格的任何变化,因为无论它永远是 TextBlock
,你不能编辑。
You have placed TextBlock
in cell template
as 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 TextBlock
which you can't edit.
在 CellEditingTemplate
中放置 TextBox
,就像这样 -
Either placed TextBox
in your CellEditingTemplate
like this -
<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>
或者只需使用 DataGridTextColumn
的 DataGridTemplateColumn
,其内部提供支持,您想通过上述代码来实现 -
Or either simply use the DataGridTextColumn
in place of DataGridTemplateColumn
which internally provides the support what are you trying to achive by the above code -
<DataGridTextColumn Header="Formulation" Width="100" IsReadOnly="False" Binding="{Binding FormulationStr}" />
这篇关于无法编辑我的DataGrid WPF Framework 4.5的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!