我有:
<DataTemplate DataType="{x:Type svgEdit:UserControlSvgEditModel}">
<svgEdit:UserControlSvgEdit />
</DataTemplate>
我想以编程方式获取类型:svgEdit:UserControlSvgEdit
我愿意:
// 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;
但我找不到如何检索关联类型?
注意:我需要以编程方式实例化模板内容并将其传递给 DockSite(对接管理器)
最佳答案
DataTemplate.LoadContent() 方法将提供 DataTemplate 中的根元素。使用它我们可以获得模板内容的数据类型。修改后的代码将如下所示,
var key = new System.Windows.DataTemplateKey(typeof(ProductsViewModel));
var dataTemplate = (DataTemplate)this.FindResource(key);
var tc = dataTemplate.LoadContent().GetType();
var instance = Activator.CreateInstance(tc);
关于c# - 如何以编程方式访问 DataTemplate 的关联数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27412932/