0

I'm trying to convert the following XAML code into its C# equivalent.

Window1.xaml:

<Window ...(omitted)>
    <Grid>
        <3rdparty:CertainComponent ...(omitted)>
            <3rdparty:CertainComponentTemplateColumn.CellTemplate>
                <DataTemplate>
                    <myComponents: MyUserComponent></myComponents: MyUserComponent>
                </DataTemplate>
            </3rdparty:GridTemplateColumn.CellTemplate>
        </3rdparty:CertainComponent>
    </Grid>
 </Window>   

This is what I have so far:

Window1.xaml.cs

public partial class Window1 : Window
{       
    // Certain fields and ViewModel stuff ommitted for simplicity  

    public Window1()
        {
            InitializeComponent();
            AddComponent();
        }
    }
    private void AddComponent()
    {
        var newComponent = new CertainComponentTemplateColumn()
        {
            MappingName = ...ommitted,
            HeaderText = ...omitted,
        };

        var dataTemplate = new DataTemplate();
        dataTemplate.??? <--- I'm lost here...
        brokerColumn.CellTemplate = dataTemplate;
            
        // code ommitted from here
    }
}

So, what I'm not sure is how to instantiate 'MyUserComponent' under DataTemplate. I have tried to use the methods under dataTemplate, but I couldn't figure out how that should be done.

I'd appreciate some assistance on this.

roccaforte
  • 81
  • 5
  • Simply don't do this. DataTemplate is meant to be defined in XAML, where you get nice IDE support. Otherwise you would have to create a XAML string and convert it to the DataTemplate instance using the [XamlReader](https://learn.microsoft.com/en-us/dotnet/api/system.windows.markup.xamlreader?view=windowsdesktop-6.0). So there is no reason to create a DataTemplate using C#. The XamlReader is meant to be used in serialization scenarios. – BionicCode Sep 14 '22 at 15:10

0 Answers0