views:

893

answers:

3

I have a tabControl that is bound to an observable collection. In the headerTemplate, I would like to bind to a string property, and in the contentTemplate I have placed a user-control.

Here's the code for the MainWindow.xaml:

<Grid>
    <Grid.Resources>            
        <DataTemplate x:Key="contentTemplate">
                <local:UserControl1 />
        </DataTemplate>

        <DataTemplate x:Key="itemTemplate">
                <Label Content="{Binding Path=Name}" />
        </DataTemplate>
    </Grid.Resources>

    <TabControl IsSynchronizedWithCurrentItem="True" 
                ItemsSource="{Binding Path=Pages}"
                ItemTemplate="{StaticResource itemTemplate}"
                ContentTemplate="{StaticResource contentTemplate}"/>

</Grid>

And its code behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = new MainWindowViewModel();
    }        
}

public class MainWindowViewModel
{
    public ObservableCollection<PageViewModel> Pages { get; set; }

    public MainWindowViewModel()
    {
        this.Pages = new ObservableCollection<PageViewModel>();
        this.Pages.Add(new PageViewModel("first"));
        this.Pages.Add(new PageViewModel("second"));
    }
}

public class PageViewModel
{
    public string Name { get; set; }

    public PageViewModel(string name)
    {
        this.Name = name;
    }
}

So the problem in this scenario (having specified an itemTemplate as well as a controlTemplate) is that I only get one instance for the user-control, where I want to have an instance for each item that is bound to.

A: 

Try setting

x:Shared="False"

When set to false, modifies Windows Presentation Foundation (WPF) resource retrieval behavior such that requests for a resource will create a new instance for each request, rather than sharing the same instance for all requests.

rudigrobler
Unfortunately, it doesn't work. I tried setting it on the contentTemplate but no luck. (not even when its defined in a separate resourceDictionary). And I'm not even allowed to set it on the userControl...
Jo-wen
Read this article by Josh Smith (http://joshsmithonwpf.wordpress.com/2009/01/27/my-mvvm-article-in-msdn-magazine/) In this article he host multiple views (usercontrols) in a tabcontrol...
rudigrobler
Also look at this article (http://www.dev102.com/2009/03/02/tidy-up-your-xaml-and-code-behind-use-usercontrols-inside-templates/) I found this morning
rudigrobler
Thanks for your replies, but in that example of Josh there's still only one instance created for the multiple views in the tabControl :s
Jo-wen
A: 

Hi, did you ever solve this? I have exactly the same problem (and so do several others, it seems, with no satisfactory responses that I can find). It seems to make no difference if you declare your ContentTemplate as a separate resource, as one person suggested. Currently I'm resorting to adding TabItems programmatically, one for each domain object in my collection, but that will become out of hand and difficult.

Thanks,

Tom

Tom
A: 

After looking for this for 2 days, I was getting desperate, but I finally found someone who had an idea on how to fix this. Actually, there's even two ways! One requires inheriting from TabControl and a custom template, the other doesn't require inheritance, but it's much more of a hack.

Hope it helps you, it fixed my problem!

Djof