WPF- TabControl. how to get DataTemplate key

Viewed 26

I have created a TabControl whose tabs are bound to UserControls

<Window x:Class="_MainWindow"...>

    ....

    <TabControl ItemContainerStyle="{StaticResource TabItemStyleVM}" x:Name="tc" Margin="1" SelectedIndex="0" Height="545" 
                    ItemsSource="{Binding Path=TabItems}" 
                    SelectedItem="{Binding Path=SelectedTabItem}"
                    IsSynchronizedWithCurrentItem="True">
        <TabControl.Resources>
            <DataTemplate x:Key="0">
                <my:myUsrControl0 x:Name="myUsrControl0" DataContext="{Binding ....}"/>
            </DataTemplate>
            <DataTemplate x:Key="1">
                <my:myUsrControl1 x:Name="myUsrControl1" DataContext="{Binding ...}"/>
            </DataTemplate>
            <DataTemplate x:Key="2">
                <my:myUsrControl2 x:Name="myUsrControl1" DataContext="{Binding ...}"/>
            </DataTemplate>
            ...
        </TabControl.Resources>
    </TabControl>

    ....

In Window CodeBehind, Im able to change TabControl content dinamically, depending on Tc SelectedIndex

Public Class _MainWindow

Private Sub _MainWindow_Loaded(ByVal sender As System.Object, ByVal e As RoutedEventArgs) Handles Me.Loaded
   ...
End Sub


Private Sub tc_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs) Handles tc.SelectionChanged
    If Not IsNothing(CType(sender, TabControl)) Then
        CType(sender, TabControl).ContentTemplate = TryCast(sender.FindResource(CType(sender, TabControl).SelectedIndex().ToString), Object)
       ...
    End If
End Sub
   ...
End Class

Now, I should place a new tab between two existing ones (eg: between tabs "1" and "2"). So, I rearranged TabControl like this

<TabControl ItemContainerStyle="{StaticResource TabItemStyleVM}" x:Name="tc" Margin="1" SelectedIndex="0" Height="545" 
                    ItemsSource="{Binding Path=TabItems}" 
                    SelectedItem="{Binding Path=SelectedTabItem}"
                    IsSynchronizedWithCurrentItem="True">
        <TabControl.Resources>
            <DataTemplate x:Key="0">
                <my:myUsrControl0 x:Name="myUsrControl0" DataContext="{Binding ....}"/>
            </DataTemplate>
            <DataTemplate x:Key="1">
                <my:myUsrControl1 x:Name="myUsrControl1" DataContext="{Binding ...}"/>
            </DataTemplate>
            <DataTemplate x:Key="10">       <----------- NEW TAB
                <my:myUsrControl10 x:Name="myUsrControl10" DataContext="{Binding ...}"/>
            </DataTemplate>
            <DataTemplate x:Key="2">
                <my:myUsrControl2 x:Name="myUsrControl2" DataContext="{Binding ...}"/>
            </DataTemplate>
            ...
        </TabControl.Resources>
    </TabControl>

at runtime, when I click on new Tab Item "10", the tc_selectionChanged detects tc.SelectedIndex = 2, and fills TabControl content to the UserControl bound to it (ie: myUsrControl2). I wish to manage TC content upon selected DataTemplate key: how can I get it?

0 Answers
Related