How To add DataGridTemplateColumn to FrameworkElementFactory(typeof(DataGrid)) programmatically? Without xaml parsing

0

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.

c#
wpf
datagrid
datagridtemplatecolumn
frameworkelementfactory
asked on Stack Overflow Oct 30, 2019 by Daniel Pfeuffer • edited Nov 12, 2019 by Cœur

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0