开发者

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?

<TabC开发者_开发百科ontrol.ContentTemplate>
    <DataTemplate>
        <Grid>
            <myCanvas:MyCanvas  Focusable="true" Margin="10" >
                <Binding Path="Canvas"></Binding>
            </myCanvas:MyCanvas>
        </Grid>
    </DataTemplate>
</TabControl.ContentTemplate>


You should use ContentPresenter

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜