Binding TabControl ItemsSource to Collection of ViewModels

后端 未结 1 1737
一整个雨季
一整个雨季 2021-01-02 08:53

For some reason I am having a heck of a time getting my TabControl to display properly when binding the ItemsSource to a ObservableCollection

相关标签:
1条回答
  • 2021-01-02 09:28

    I ended up using a ContentControl with a TabControl data template (like the original tutorial project). Here is the xaml code I ended up with. I did not change the code-behind from the original sample project to make this work. The ContentControl is in my MainWindow.xaml and the other two pieces of code were in a ResourceDictionary.

    <!-- Workspaces Tab Control -->
          <ContentControl Grid.Row="1"
                          VerticalAlignment="Stretch"
                          HorizontalAlignment="Stretch"
                          Content="{Binding Path=Workspaces}"
                          ContentTemplate="{StaticResource WorkspacesTemplate}"/>
    
    <!-- Workspaces Template -->
      <DataTemplate x:Key="WorkspacesTemplate">
        <TabControl Style="{StaticResource ClosableTabControl}"
                    IsSynchronizedWithCurrentItem="True"
                    ItemsSource="{Binding}"
                    ItemTemplate="{StaticResource WorkspaceTabItemTemplate}"
                    Margin="1"/>
      </DataTemplate>
    
    
    <!-- Workspace Tab Item Template -->
      <DataTemplate x:Key="WorkspaceTabItemTemplate">
        <Grid Width="Auto" Height="Auto">
          <ContentPresenter ContentSource="Header" Margin="3" 
                            Content="{Binding Path=DisplayName}"
                            HorizontalAlignment="Center" VerticalAlignment="Center">
            <ContentPresenter.Resources>
              <Style TargetType="TextBlock">
                <Setter Property="Foreground" Value="{StaticResource Foreground}"/>
              </Style>
            </ContentPresenter.Resources>
          </ContentPresenter>
        </Grid>
      </DataTemplate>
    
    0 讨论(0)
提交回复
热议问题