I have:
<DataTemplate DataType="{x:Type svgEdit:UserControlSvgEditModel}">
<svgEdit:UserControlSvgEdit />
</DataTemplate>
I want to get programmatically the type: svgEdit:UserControlSvgEdit
I do:
// Here the obj Type is the key to the resource, it works but
var key = new System.Windows.DataTemplateKey(obj.GetType());
var dataTemplate = (DataTemplate)DockSite.FindResource(key);
// I don't know how to get the DataTemplate associated type ????
var tc = dataTemplate.Template as TemplateContent;
But I can't find how to retrieve the associated type ?
Note: I need to instanciate the template content programatically and pass it to a DockSite (Docking Manager)
DataTemplate.LoadContent() method will give the root element within the DataTemplate.Using that we can get the data type of the template content. The modified code will look like this,
var key = new System.Windows.DataTemplateKey(typeof(ProductsViewModel));
var dataTemplate = (DataTemplate)this.FindResource(key);
var tc = dataTemplate.LoadContent().GetType();
var instance = Activator.CreateInstance(tc);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With