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.
精彩评论