I have a WPF grid with autogeneratedcolumn set to true. I have to modify the template of a given cell for every row in the grid. Is there are an event in WPF datagrid which can allow me to capture every row and perform the necessary modifications before getting it rendered?
If not, then can you please suggest what could be the best way of customizing row cell's in an autogenerated datagrid?
I recommend using a DataTemplateSelector
instead of messing with events. This will allow you to cleanly vary the cell template based on data in the cell programatically. Hooking events in WPF inevitably leads to references getting held far long than you expect which manifests as memory leaks.
MSDN documentation is available here.
Code From MSDN:
Namespace SDKSample
Public Class TaskListDataTemplateSelector
Inherits DataTemplateSelector
Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
Dim element As FrameworkElement
element = TryCast(container, FrameworkElement)
If element IsNot Nothing AndAlso item IsNot Nothing AndAlso TypeOf item Is Task Then
Dim taskitem As Task = TryCast(item, Task)
If taskitem.Priority = 1 Then
Return TryCast(element.FindResource("importantTaskTemplate"), DataTemplate)
Else
Return TryCast(element.FindResource("myTaskTemplate"), DataTemplate)
End If
End If
Return Nothing
End Function
End Class
End Namespace