简体   繁体   中英

C# WPF - Scrollbar in dynamic TabControl

I'm trying to add a Scrollbar for each TabItem so when there is too much content i can scroll down. I've tried to set it to visible but it doesn't work.

The TabItems will be added dynamically over the code with Header and Content.

Any ideas to add a Scrollbar horizontally and vertically over code or XAML?

<TabControl x:Name="SemesterTabs" Margin="20,61,20,55" Width="584" HorizontalAlignment="Left">
    <TabControl.ContentTemplate>
        <DataTemplate>
            <StackPanel>
                <!-- Tabs mit Datagridview binden und nur ein Eintrag in den Tabs auswaehlbar machen -->
                <DataGrid IsReadOnly="True" SelectionMode="Single" FontStyle="Normal" ItemsSource="{Binding DefaultView}" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" SelectionChanged="DataGrid_SelectionChanged">
                    <!-- Um Faecher mit Rechtsclick einfuegen, bearbeiten und loeschen zu koennen -->
                    <DataGrid.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Hinzufügen" Click="addNewRow_Click"/>
                            <MenuItem Header="Bearbeiten" Click="editRowEntry_Click"/>
                            <MenuItem Header="Löschen" Click="deleteRowEntry_Click"/>
                        </ContextMenu>
                    </DataGrid.ContextMenu>
                </DataGrid>
            </StackPanel>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

The StackPanel is the problem, you have to remove it.

To understand why, it helps to think of panels and container controls as containers that have an external size, ie the size they take up in their container, and an internal size, ie the space they provide for their children.

For example, in a Grid , the external size and the internal size are the same. But in a ScrollViewer , the internal size may be infinite, while its external size is not.

The same is true for a StackPanel , say with a vertical orientation. The internal width of such a panel is the same as its external width, but its internal height is infinite.

Now, when you place a ScrollViewer in such a panel, it may present a horizontal scrollbar, as needed to provide space for its content, but it will never present a vertical scrollbar. It doesn't need to, as its external height can grow to infinite, and because of that, so can its internal height without having to use a scrollbar.

The Problem was the Stackpanel. Somehow it was blocking the Scrollviewer in my TabItems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM