Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using data binding on value which is a FrameworkElement

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.

like image 687
JaredPar Avatar asked Apr 02 '10 07:04

JaredPar


1 Answers

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.

like image 189
Timores Avatar answered Oct 05 '22 06:10

Timores