A common requirement for a DataGrid is to have entire rows that are read-only. You may have a list of records in your DataGrid and some need to be locked for various reasons. Unfortunately, in the XAML definition of a DataGrid, there isn’t a way to mark a row as being Read-Only. To accomplish this without tying my view to my model or rewriting the DataGrid, I’m utilizing the BeginningEdit event, the Tag property of the DataGridRow, and created a style for the DataGridRow with a DataTrigger to put everything together.
First thing I did was to make sure my Model had an IsReadOnly property to latch onto, in the attached sample code, I did this by adding the property to one of the Customer in the Northwind database .
Next, using a Trigger in my DataGridRow style, if the data item’s IsReadOnly property evaluates to “True” then I set the DataGridRow ‘s Tag to a string I can look for in my BeginningEdit event handler. In the sample project attached and in the code snippets below, I used the string “ReadOnly”. For a visual cue, I also set the HeaderTemplate to show an image to indicate that the row is locked.
Finally, in my BeginningEdit event handler for my DataGrid, I look at the Row going into Edit Mode, if its Tag property has been set to “ReadOnly” I cancel the event so it never makes it into edit mode. This enforces the IsReadOnly property from my mode.