WPF/VB.NET how to set TabControl Resources Template at runtime

Antonio C 21 Reputation points
2022-09-05T10:30:31.31+00:00

I have built this TabControl

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

<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:MainControl x:Name="MainControl" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}, AncestorLevel=1}, Path=DataContext.MyData}"/>
</DataTemplate>
<DataTemplate x:Key="1">
<my:Details1Control x:Name="Details1Control" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}, AncestorLevel=1}, Path=DataContext.MyData}"/>
</DataTemplate>
...
</TabControl.Resources>
</TabControl>
...
</Window>

I wish to set content of <DataTemplate x:Key="2"> at runtime, depending over some condition. Eg:

Public Class _ViewData

Private Sub _DatiGenerali_Loaded(ByVal sender As System.Object, ByVal e As RoutedEventArgs) Handles Me.Loaded
If [user.profile] = "1" Then
[set tc.DataTemplate(1) = "Details1Control.xaml"]
Else
[set tc.DataTemplate(1) = "Details2Control.xaml"]
End If

End Sub
End Class

is there a way to get it?

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
767 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 40,586 Reputation points Microsoft Vendor
    2022-09-06T07:13:43.99+00:00

    Hi,@Antonio C . Welcome Microsoft Q&A.
    Do you want the following effect? If not, could you show your complete code that reproduces the problem and explain the problem in detail?

     <Window.Resources>  
            <DataTemplate x:Key="Tab1Template">  
                <Grid>  
                    <Rectangle Fill="AliceBlue"/>  
                    <ContentPresenter Content="{Binding }"/>  
                </Grid>  
            </DataTemplate>  
            <DataTemplate x:Key="Tab2Template">  
                <Grid>  
                    <Rectangle Fill="Pink"/>  
                    <ContentPresenter Content="{Binding}"/>  
                </Grid>  
            </DataTemplate>  
            <local:TabItemTemplateSelector  x:Key="TabItemTemplateSelector"  Tab1Template="{StaticResource Tab1Template}"  Tab2Template="{StaticResource Tab2Template}" />  
        </Window.Resources>  
        <Grid>  
            <TabControl Name="tb" SelectedIndex="0" ItemsSource="{Binding Tabs}"  ContentTemplateSelector="{StaticResource TabItemTemplateSelector}" >  
            </TabControl>  
      
        </Grid>  
    

    Codebehind:

    Public Partial Class MainWindow  
        Inherits Window  
      
        Public Sub New()  
            InitializeComponent()  
            DataContext = New MainVM()  
        End Sub  
    End Class  
      
    Public Class TabItemVM  
        Public Property Name As String  
      
        Public Overrides Function ToString() As String  
            Return Name  
        End Function  
    End Class  
      
    Public Class MainVM  
        Public Property Tabs As ObservableCollection(Of TabItemVM)  
      
        Public Sub New()  
            Tabs = New ObservableCollection(Of TabItemVM)() From {  
                New TabItemVM() With {  
                    .Name = "Tab1"  
                },  
                New TabItemVM() With {  
                    .Name = "Tab2"  
                }  
            }  
        End Sub  
    End Class  
      
    Public Class TabItemTemplateSelector  
        Inherits DataTemplateSelector  
      
        Public Property Tab1Template As DataTemplate  
        Public Property Tab2Template As DataTemplate  
      
        Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate  
            If item Is Nothing Then Return Nothing  
            Dim tabItem = TryCast(item, TabItemVM)  
            If tabItem.Name = "Tab1" Then Return Tab1Template  
            If tabItem.Name = "Tab2" Then Return Tab2Template  
            Return MyBase.SelectTemplate(item, container)  
        End Function  
    End Class  
    

    ---------------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful