Click here to Skip to main content
15,904,986 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a DataGrid with a DataGridTemplateColumn. I'm not able to edit the Comment field. When I double click on a Comment field, the background changes a expected to LightSalmon and the content of the bound property Comment is shown but I cannot edit the content.

What I have tried:

<DataGridTemplateColumn IsReadOnly="False" Header="Comment" MinWidth="80">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock
              Background="LightGray"
                Margin="5"
                MinHeight="35"
                TextAlignment="Center"
                Text="{Binding Comment, Mode=OneWay}"
                IsEnabled="True"
                />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <TextBlock
              Background="LightSalmon"
                Opacity="0.5"
                Margin="5"
                MinHeight="35"
                TextAlignment="Center"
                Text="{Binding Comment}"
                IsEnabled="True"
                />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
Posted
Updated 16-Feb-24 4:05am
v6
Comments
Richard Deeming 16-Feb-24 9:36am    
Why not?

Remember, we can't see your screen, we can't access your computer, we can't run your project, and we can't read your mind. All we have to work with is the information you provide here.

So help us to help you. Click the green "Improve question" link and update your question to explain precisely what the problem is. Include the full details of any errors.
[no name] 16-Feb-24 11:43am    
A "TextBlock" isn't "editable" ... you need a "TextBox".

1 solution

Gerry gave the solution, to elaborate on taht -

The TextBlock is a read-only control, and that's why you can't edit the content when you double-click on it - MS Learn | TextBlock Class[^]

To use editing, you should use an editable control such as 'TextBox' in your 'CellEditingTemplate' -
XAML
<DataGridTemplateColumn IsReadOnly="False" Header="Comment" MinWidth="80">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock
                Background="LightGray"
                Margin="5"
                MinHeight="35"
                TextAlignment="Center"
                Text="{Binding Comment, Mode=OneWay}"
                IsEnabled="True"
            />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <TextBox
                Background="LightSalmon"
                Margin="5"
                MinHeight="35"
                TextAlignment="Center"
                Text="{Binding Comment, UpdateSourceTrigger=PropertyChanged}"
                IsEnabled="True"
            />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>


The 'TextBox' in the 'CellEditingTemplate' allows for your user input, and the 'UpdateSourceTrigger=PropertyChanged' makes sure that the source property gets updated as your user types.
 
Share this answer
 
Comments
4711a 16-Feb-24 15:44pm    
Thank you for your advice and I apologize for not looking up the TextBlock.
Andre Oosthuizen 16-Feb-24 15:47pm    
No need to apologize, we all go through our stages...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900