I want create a datagrid programmatically like this
var element = new FrameworkElementFactory(typeof(DataGrid));
if (reflectionInfo.Type.GenericTypeArguments.Count() == 1)
{
var subType = reflectionInfo.Type.GenericTypeArguments[0];
var fields = subType.GetFieldsRecursive("");
foreach (var field in fields)
{
var column = new FrameworkElementFactory(typeof(DataGridTextColumn));
var bindingCol = new Binding(field.Path);
bindingCol.Mode = BindingMode.TwoWay;
bindingCol.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
column.SetBinding(TextBlock.TextProperty, bindingCol);
element.AppendChild(column);
}
}
return element;
but that throws an exception:
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)
I know FrameworkElementFactory is deprecated but the easiest way for me. Is there a solution with FrameworkElementFactory or with Xaml parse to cast into FrameworkElementFactory.
User contributions licensed under CC BY-SA 3.0