How to make certain cells editable in the DataGrid

Feb 11, 2009 at 1:11 PM
I have a scenario in which I am creating columns dynamically and then binding them to an object model, all code behind.

What I'm trying to do now is to make some cells in the grid editable, but not the whole column. I have a property on the data objects called IsWritable that should be used to make the decision. What is the best way of doing this? I've tried using a DataTrigger on the style template and extending the DataGridTemplateColumn to allow for binding, but I can't seem to get it all to work.
Feb 11, 2009 at 1:23 PM
Look at the BeginningEdit event, and search the archives here for more information.
Feb 11, 2009 at 1:41 PM
Thank you for you reply. I have tried searching the forums but didn't find anything useful.

Are you suggesting that I should listen to the BeginningEdit event and then look at the data object and decide whether it should be editable at that point? Wouldn't that make it impossible to style the cells to somehow show which ones are editable? It seems a bit like a "hack", I would prefer to do this with XAML if at all possible.
Feb 12, 2009 at 1:38 PM
FYI I got this figured out now.

I created a style with a DataTrigger set to change the ContentTemplate of the DataGridCell to either a TextBlock or a TextBox depending on my IsWritable property. This however did not allow for two-way binding, which forced me to build the style for each column code-behind using XamlReader.Load so I could specify the required binding path to each object.

A bit dirty perhaps but I really couldn't figure out a better way of doing it. I ended up not having to use the DataGridTemplateColumn at all.