Hosting WPF Controls in a XamDataGrid Cell

Andrew Smith [Infragistics] / Friday, March 27, 2009

By default, the elements used to display and edit values in a XamDataGrid cell are based on the ValueEditor associated with that Field. The EditorType and/or EditorStyle is used to determine which ValueEditor derived class (e.g. XamTextEditor, XamMaskedEditor, etc.) should be used. If those are not specified a default one is chosen based on the resolved EditAsType.

One way to provide a control for a cell is to create a custom Template for the cell as part of the CellValuePresenterStyle on the FieldSettings. This works fine if you are just using this to render the value but it has limitations with regards to editing and tab navigation. Since ValueEditor is not being used the EditMode(Start|End)(ing|ed) events will not be raised. Also when the cell tries to go into edit mode, the controls within the cell will not automatically get focus and when you use the tab key to navigate, the grid will use that to navigate amongst the cells.

The other option is to create a custom ValueEditor and use that as the editor for the Field. To help facilitate this I have created a sample ValueEditor named ControlHostEditor that can be used to host arbitrary controls/elements. The ControlHostEditor exposes 2 DataTemplate properties - RenderDataTemplate and EditDataTemplate - which are used based on whether the editor is in edit mode. You can get the sample here.

Update [8/25/09]: I modified the sample to avoid using FindAncestor within the sample templates since a FindAncestor binding is not evaluated synchronously. Instead, there is now an inherited property that provides access to the editor in the descendant elements eliminating the need for the FindAncestor call.