views:

149

answers:

1

I would like to build a TabControl in Silverlight which is driven by a collection of objects. I'll show the code below of a VERY basic setup that I'm trying to prototype.

MainPage.xaml

<UserControl x:Class="DataDrivenTabControl.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:local="clr-namespace:DataDrivenTabControl"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<UserControl.DataContext>
    <local:MainPage_ViewModel/>
</UserControl.DataContext>

<StackPanel>
    <controls:TabControl>
        <!-- What do I need to do here for a Template? -->
    </controls:TabControl>
</StackPanel>

MainPage_ViewModel.cs

public class MainPage_ViewModel : Base_ViewModel
{
    public MainPage_ViewModel()
    {
        PopulateCollectionOfTabs();
    }

    public ObservableCollection<TabItem_DataViewModel> CollectionOfTabs
    {
        get { return collectionOfTabs; }
        set
        {
            if (collectionOfTabs != value)
            {
                collectionOfTabs = value;
                OnPropertyChanged("CollectionOfTabs");
            }
        }
    }
    private ObservableCollection<TabItem_DataViewModel> collectionOfTabs = new ObservableCollection<TabItem_DataViewModel>();

    private void PopulateCollectionOfTabs()
    {
        this.CollectionOfTabs.Add(
            new TabItem_DataViewModel()
            {
                TabDescription = "Tab 1",
                ButtonDescription = "Button for Tab 1"
            });

        this.CollectionOfTabs.Add(
            new TabItem_DataViewModel()
            {
                TabDescription = "Tab 2",
                ButtonDescription = "Button for Tab 2"
            });
    }
}

TabItem_DataViewModel.cs

public class TabItem_DataViewModel : Base_ViewModel
{
    public string TabDescription
    {
        get { return tabDescription; }
        set
        {
            if (tabDescription != value)
            {
                tabDescription = value;
                OnPropertyChanged("TabDescription");
            }
        }
    }
    private string tabDescription = string.Empty;

    public string ButtonDescription
    {
        get { return buttonDescription; }
        set
        {
            if (buttonDescription != value)
            {
                buttonDescription = value;
                OnPropertyChanged("ButtonDescription");
            }
        }
    }
    private string buttonDescription = string.Empty;
}

All I'm really trying to do in this example is to get the TabControl to show up with a dynamic list of tabs which would be bound to a collection ("Tab 1" & "Tab 2" in the header for the current implementation). When you click on a Tab, there could be a button (for simplicity), where the content of the button would bind to ButtonDescription on the TabItem_DataViewModel. This is very basic, but if I can get this to work, I'll for sure be able to implement the rest of my solution.

I'm sure this has to be done with templates on the TabControl, but I just left it empty hoping that someone would be able to point me in the right direction.

Any help will be greatly appreciated, thanks!

A: 

I was able to get it to work as expected by using Telerik's RadTabControl as shown below. I used Telerik's version since the ItemSource is an IEnumerable, not a collection of TabItems.

<telerikNavigation:RadTabControl ItemsSource="{Binding CollectionOfTabs}">
        <telerikNavigation:RadTabControl.ItemContainerStyle>
            <Style TargetType="telerikNavigation:RadTabItem">
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding TabDescription}"/>
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Button Content="{Binding ButtonDescription}" Width="100" HorizontalAlignment="Center"/>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </telerikNavigation:RadTabControl.ItemContainerStyle>
    </telerikNavigation:RadTabControl>
JSprang