Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何以编程方式将DataGridTemplateColumn添加到FrameworkElementFactory(typeof(DataGrid))中?没有xaml解析_C#_Wpf_Datagrid_Datagridtemplatecolumn_Frameworkelementfactory - Fatal编程技术网

C# 如何以编程方式将DataGridTemplateColumn添加到FrameworkElementFactory(typeof(DataGrid))中?没有xaml解析

C# 如何以编程方式将DataGridTemplateColumn添加到FrameworkElementFactory(typeof(DataGrid))中?没有xaml解析,c#,wpf,datagrid,datagridtemplatecolumn,frameworkelementfactory,C#,Wpf,Datagrid,Datagridtemplatecolumn,Frameworkelementfactory,我希望像这样以编程方式创建datagrid var-element=newframeworkelementfactory(typeof(DataGrid)); if(reflectionInfo.Type.GenericTypeArguments.Count()==1) { var subType=reflectionInfo.Type.GenericTypeArguments[0]; 变量字段=子类型。GetFieldsRecursive(“”); foreach(字段中的变量字段) { va

我希望像这样以编程方式创建datagrid

var-element=newframeworkelementfactory(typeof(DataGrid));
if(reflectionInfo.Type.GenericTypeArguments.Count()==1)
{
var subType=reflectionInfo.Type.GenericTypeArguments[0];
变量字段=子类型。GetFieldsRecursive(“”);
foreach(字段中的变量字段)
{
var column=newframeworkelementfactory(typeof(DataGridTextColumn));
var bindingCol=新绑定(field.Path);
bindingCol.Mode=BindingMode.TwoWay;
bindingCol.UpdateSourceTrigger=UpdateSourceTrigger.PropertyChanged;
column.SetBinding(TextBlock.TextProperty,bindingCol);
元素。追加子元素(列);
}
}
返回元素;
但这带来了一个例外:

System ArgumentException
  HResult=0x80070057
  Message = The type "DataGridTemplateColumn" must be derived from "FrameworkElement", "FrameworkContentElement" or "Visual3D".
  Source = PresentationFramework
  Stack monitoring:
   for System.Windows.FrameworkElementFactory.set_Type(Type value)
   for System.Windows.FrameworkElementFactory..ctor(Type type, String name)
   for System.Windows.FrameworkElementFactory..ctor(Type type)
我知道FrameworkElementFactory已被弃用,但对我来说这是最简单的方法。
有没有一个使用FrameworkElementFactory或Xaml parse的解决方案可以强制转换到FrameworkElementFactory中。

嘿!您的标题和问题似乎不一致:一个是“无XAML解析”,另一个是“或有XAML解析”。如果您沿着解析路线走下去,就不需要返回到工厂,只需要返回到控件本身。你能澄清一下吗?很抱歉,我只需要FrameworkElementFactory作为返回类型,如果有必要,我可以使用XAML解析。