morsanu
morsanu

Reputation: 985

WPF Canvas Binding

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>

Upvotes: 3

Views: 5880

Answers (2)

bniwredyc
bniwredyc

Reputation: 8829

You should use ContentPresenter

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

Upvotes: 5

Ben Von Handorf
Ben Von Handorf

Reputation: 2336

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.

Upvotes: 2

Related Questions