views:

60

answers:

1

One of my data sources produces a collection of values which are typed to the following interface

public interface IData
{
    string Name { get; }
    FrameworkElement VisualElement { get; }
}

I'd like to use data binding in WPF to display a collection of IData instances in a TabControl where the Name value becomes the header of the tab and the VisualElement value is displayed as the content of the corresponding tab.

Binding the header is straight forward. I'm stuck though on how to define a template which allows me to display the VisualElement value. I've tried a number of solutions with little success. My best attempt is as follows.

    <TabControl ItemsSource="{Binding}">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding Name}"/>
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate>
                How do I display VisualElement here?
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl> 

I'm still very new to WPF so I could be missing the obvious here.

+3  A: 

ContentPresenters were made for this. The content template becomes:

        <TabControl.ContentTemplate>
            <DataTemplate>
                <ContentPresenter Content="{Binding VisualElement}" />
            </DataTemplate>
        </TabControl.ContentTemplate>

I tested it with a TextBlock and a TextBox.

Timores
Worked like a champ! Thanks!
JaredPar