views:

247

answers:

2

Hey guys,

I'm rather new to WPF, so maybe this is a simple question. I have a class that derives from Canvas, let's call it MyCanvas. And I have a class, MyClass, that has a property of type MyCanvas. In XAML, I built a TabControl, so each TabItem binds to a MyClass object. Now, in the Content of every tab I want to display MyObject.MyCanvas.

How should I do that?

<TabControl.ContentTemplate>
    <DataTemplate>
        <Grid>
            <myCanvas:MyCanvas  Focusable="true" Margin="10" >
                <Binding Path="Canvas"></Binding>
            </myCanvas:MyCanvas>
        </Grid>
    </DataTemplate>
</TabControl.ContentTemplate>
+1  A: 

You should use ContentPresenter

<TabControl.ContentTemplate> 
    <DataTemplate> 
        <Grid> 
            <ContentPresenter Content="{Binding MyCanvas}" Focusable="true" Margin="10" />
        </Grid> 
    </DataTemplate> 
</TabControl.ContentTemplate>
bniwredyc
A: 

Try using ContentPresenter and binding the contents to the property you want. If the property is a descendent of Canvas, this should result in it simply displaying that content. If the property was of another type, it would attempt to use a DataTemplate to render it.

Ben Von Handorf
Thanks for the correct answer, but I marked bniwredyc's as the solution because it was the first.
morsanu
And more complete. No worries... I voted up his answer as soon as I saw it. :)
Ben Von Handorf