WPF DataTemplate: instance of UserControl from code behind

Viewed 17

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.

0 Answers
Related