0

I have a DataTemplate on my App.XAML

        <DataTemplate DataType="{x:Type ViewModels:MSMemberViewModel}">
            <Views:MSMemberView>
            </Views:MSMemberView>
        </DataTemplate>

Which I use to create views in a tabcontrol, inside another view:

    <TabControl ItemsSource="{Binding Tabs}" SelectedIndex="{Binding SelectedIndex}">
        <TabControl.ItemTemplate>

  //...
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate>
                <ContentControl Content="{Binding ViewModel}">

                </ContentControl>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>

The Tabs is an ObservableCollection of objects containing the tab name and the ViewModel.

New ViewModels are instantiated every time I add a new tab to the ObservableCollection (I know this because the constructor is called everytime I add a new tab), and my code to number the tab names works (because every tab has a different name), but the View is duplicated! How do I get around this?

Edit:

Also, none of the ViewModels on the ObservableCollection reflect the current values of the instantiated ViewModel/View on debug.

Tyress
  • 3,573
  • 2
  • 22
  • 45
  • Do you mean that the information in the newly created view is copied from the previously existing one? – XAMlMAX Sep 28 '15 at 11:15
  • Not really copied, it's the same. The constructor for the ViewModel is called but the view is completely the same – Tyress Sep 28 '15 at 11:18
  • The difference is that if I highlight a textbox on one tab, it will steal the highlight from the other. Plus the tab name is different (set on ItemTemplate) – Tyress Sep 28 '15 at 11:25

1 Answers1

0

Hmm since I don't know how the field binding is implemented, this is only a wild guess but I think you should change the DataTemplate a bit. Like this...

<!-- your stuff -->
<TabControl.ContentTemplate>
   <DataTemplate>
       <ContentControl DataContext="{Binding ViewModel}">

       </ContentControl>
   </DataTemplate>
</TabControl.ContentTemplate>
<!-- your stuff -->
DHN
  • 4,807
  • 3
  • 31
  • 45
  • Not working for me, it doesn't show anything. Not sure what you mean by field binding (is it ViewModel bindings on the actual View)? Doing shows the view, but when I switch tabs, the view is exactly the same. – Tyress Sep 29 '15 at 02:53