Mathias Colpaert
Mathias Colpaert

Reputation: 680

Create contentcontrol in code behind

How to create following Border with contentcontrol in code?

<Border Name="Test">
        <ContentControl ContentTemplate="{StaticResource _cardDataTemplate}" Content="{Binding}"/>
</Border>

I am using following code, but the datatemplate is not applied:

Border newCard = new Border();
newCard.Child = new ContentControl()
{
    ContentTemplate = CardDataTemplate
};

CardDataTemplate is assigned, and is not the problem as far as I know.

Upvotes: 2

Views: 1750

Answers (1)

Alexis
Alexis

Reputation: 825

In your case you need to create a binding for the Content property to DataContext ( Content="{Binding}" in XAML). You can do it by writing the following code:

BindingOperations.SetBinding(yourContentControlInstance, ContentControl.ContentProperty, new Binding());

Upvotes: 3

Related Questions