Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access programmatically the associated dataType of a DataTemplate?

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)

like image 555
Eric Ouellet Avatar asked Dec 10 '14 23:12

Eric Ouellet


1 Answers

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);
like image 126
Jawahar Avatar answered Oct 11 '22 11:10

Jawahar