This is a migrated thread and some comments may be shown as answers.

TabControl with ItemSource

1 Answer 89 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
Jerome
Top achievements
Rank 1
Jerome asked on 29 Jan 2013, 05:42 AM
This is probably a basic WPF question, and not specifically Telerik related, but I figured I'd ask here. I am trying to create a reusable control, right now named SectionLayout, which allows the user of the control to define "sections", with Visuals in each section. I want to render this using RadTabControl.

Basically, the consumer of the SectionLayout would be using it like this:

<local:SectionLayout>
  <local:Section Name="Foo">
    <Button Text="{Binding Foo}" />
  </local:Section>
</local:SectionLayout>

I've already got most of it working. SectionLayout has a "Sections" property, of type SectionCollection, that holds items of type Section. Section has Name and Content properties. Name is a string, Content is an object.

The ControlTemplate for SectionLayout currently looks like this:

<ControlTemplate>
<telerik:RadTabControl
    Align="Right"
    TabOrientation="Vertical"
    TabStripPlacement="Left"
    ItemsSource="{TemplateBinding Sections}">
    <telerik:RadTabControl.ItemContainerStyle>
        <Style
            TargetType="{x:Type telerik:RadTabItem}">
            <Setter
                Property="MinHeight"
                Value="150" />
            <Setter
                Property="MinWidth"
                Value="30" />
        </Style>
    </telerik:RadTabControl.ItemContainerStyle>
    <telerik:RadTabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock
                Text="{Binding Name}" />
        </DataTemplate>
    </telerik:RadTabControl.ItemTemplate>
    <telerik:RadTabControl.ContentTemplate>
        <DataTemplate
                DataType="{x:Type local:Section}">
            <ContentPresenter
                Margin="4,4,4,4"
                DataContext="{TemplateBinding DataContext}"
                Content="{Binding Content}" />
        </DataTemplate>
    </telerik:RadTabControl.ContentTemplate>
</telerik:RadTabControl>
</ControlTemplate>

So, basically I'm just making an easier interface to use a RadTabControl. So I can reuse it in other places. Though it might change from being a RadTabControl to something else in the future.

The problem I'm having is getting the original DataContext of Section.Content property to flow into the RadTabControl.ContentTemplate. The ContentTemplate is of course given the business object (Section) as it's datacontext. So, I can easily present Section.Content. However, when I do so, Section.Content begins inheriting the DataContext given to it by the ContentPresenter, which happens to be Section.Content.

How can I do this properly? I want the user to be able to use my control as he would any normal layout control: Grid, Canvas, StackPanel, whatever. But, I need to use a TabControl internally to do the layout.

1 Answer, 1 is accepted

Sort by
0
Pavel R. Pavlov
Telerik team
answered on 31 Jan 2013, 04:08 PM
Hi Jerome,

I am not sure that I understand your scenario correctly. However, I will suggest you let the RadTabControl handle the delivering of the DataContext by itself. You can do this by removing the TemplateBinding from the ContentPresenter and the DataType from the DataTemplate of your RadTabControl.ContentTemplate. Hopefully it will work for you. In case that this approach doesn't work, I will kindly ask you to send me your project so I will be able to suggest the best approach in your scenario. Thank you for your cooperation.

Regards,
Pavel R. Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
TabControl
Asked by
Jerome
Top achievements
Rank 1
Answers by
Pavel R. Pavlov
Telerik team
Share this question
or