When you assign an object to a Content control it will materialize a Visual appropriate for that assigned object. Is there a programmatic way to achieve the same result? I would like to call a function in WPF with an object and get back a Visual, where the same logic is applied in generating the Visual as if you had supplied the object to a Content control instance.
For example, if I have a POCO object and assign it to a Content control and there happens to be an appropriate DataTemplate defined then it materializes that template to create the Visual. I would like my code to be able to take a POCO object and get back from WPF the Visual.
Any ideas?
Use DataTemplate.LoadContent(). Example:
http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.loadcontent.aspx
If you have a DataTemplate defined for all instances of a type (DataType={x:Type …}, but no x:Key=”…”) then you can create content using the appropriate DataTemplate using the following static method. This method also emulates ContentControl by returning a TextBlock if no DataTemplate is found.