0

My question beyond to get an instance, is how to get this instance as wpf UserControl.

I have a UserControl (Holder) that can show another UserControls (childs) into it, those controls are into same assembly, but user can select which ones must place into holder.

I´m creating the instances as this:

        CapturasAdicionalesControl ctr = d as CapturasAdicionalesControl;
        List<ICapturaAdicional> capturas = ctr.Capturas;
        string assemblyName = "MyAssemblyPath";

        Assembly assembly = Assembly.Load(assemblyName);

        foreach (ICapturaAdicional captura in capturas)
        {
            Type capturaObj = assembly.GetType($"{assemblyName}.{captura.Control}");
            InsertCaptura(capturaObj);
        }

I get my type, but when try to add into Holder it generates an invalid cast from type to UserControl.

I was trying to use (UserControl)MyType ... MyType as UserControl with no results.

Now my questions are:

  1. How to cast / convert my type (allready a usercontrol) to UserControl

if one isn't posible

  1. How to get my user control as Usercontrol directly from the assembly or how to create an instance of it if only have it's name?

  2. Another approach would be very appreciated?

EDIT 1

    private static Grid InsertCaptura(UserControl instance)
    {
        Grid NewGrid = new Grid()
        {
            Style = (Style)Application.Current.FindResource("TabOptionBackGroundStyle")
        };
        NewGrid.RowDefinitions.Add(new RowDefinition() { });
        NewGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
        NewGrid.RowDefinitions.Add(new RowDefinition() { });

        NewGrid.ColumnDefinitions.Add(new ColumnDefinition() { });
        NewGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
        NewGrid.ColumnDefinitions.Add(new ColumnDefinition() { });

        Grid.SetColumn((UIElement)instance, 1);
        Grid.SetRow((UIElement)instance, 1);

        NewGrid.Children.Add((UIElement)instance);

        return NewGrid;

    }
Juan Pablo Gomez
  • 5,203
  • 11
  • 55
  • 101

0 Answers0