Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5 views
in Technique[技术] by (71.8m points)

xaml - Cannot edit cells of my DataGrid WPF Framework 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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>

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}" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...