滚动 WPF TabItems(不是整个 TabControl)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25131204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:16:51  来源:igfitidea点击:

Scrolling for WPF TabItems (not entire TabControl)

c#.netwpfxamlvsto

提问by bbqchickenrobot

I have the following xaml:

我有以下 xaml:

<UserControl x:Class="MyProject.Word.Addin.Presentation.MainTaskPane" 
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:MyProject.Word.Addin.Presentation" 
         mc:Ignorable="d">
<d:UserControl.DataContext>
    <local:MyProjectPaneViewModelHandler />
</d:UserControl.DataContext>
<!--<Grid>-->

    <DockPanel Name="MainDockPanel" Background="red">
        <local:ExToolBar DockPanel.Dock="Top" />
        <Button DockPanel.Dock="Top" Click="ButtonGetHeightDimensions" Content="Show Dimensions" Height="40"></Button>
        <TabControl DockPanel.Dock="Top" x:Name="TabControl1" Background="LightSkyBlue">
            <TabItem x:Name="Tab1" Background="LightGreen"> 
                <TabItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <Ellipse Width="10" Height="10" Fill="DarkGray"/>
                        <TextBlock>Filters</TextBlock>
                    </StackPanel>
                </TabItem.Header>
                <ScrollViewer Name="ScrollViewer1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                <StackPanel Name="Tab1StackPanel" Orientation="Vertical" MaxHeight="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TabControl}}, Path=ActualHeight}" >
                        <TextBlock FontSize="24" FontWeight="Bold" Foreground="DarkSlateGray" FontStyle="Normal">
                        Filters
                    </TextBlock>
                    <Button x:Name="ClearFiltersButton" Click="ClearFilters_OnClick" Background="DarkRed" Foreground="White"
                            FontSize="20" FontWeight="Bold" MaxWidth="124" HorizontalAlignment="Left">
                        Clear Filters
                    </Button>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock>
                            <Run>Total Paragraphs </Run><Run Text="{Binding ResearchLanguageViewModel.TotalCount}"></Run>
                        </TextBlock>
                    </StackPanel>
                    <ItemsControl ItemsSource="{Binding ResearchLanguageViewModel.Filters, Mode=OneWay}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>

                                <StackPanel>
                                    <TextBlock Text="{Binding Path=Type}"></TextBlock>
                                    <TextBox Text="Search...."></TextBox>
                                    <ItemsControl ItemsSource="{Binding Path=Values, Mode=OneWay}">
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <CheckBox Content="{Binding Mode=OneWay}"></CheckBox>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
                    </ScrollViewer>
            </TabItem>
            <TabItem x:Name="Tab2">
                <TabItem.Header>
                        <TextBlock>A 2nd Tab</TextBlock>
                </TabItem.Header>
                <StackPanel Orientation="Horizontal">
                        <TextBlock>
                            <Run>Mama always said lifes like a box of chocolates...</Run>
                        </TextBlock>
                </StackPanel>
            </TabItem>
        </TabControl>
    </DockPanel>
<!--</Grid>-->

And the following objects....

和以下对象....

public class FilterViewModel
{
    public string Type { get; set; }
    public ObservableCollection<string> Values { get; set; }
}

public class ResearchLanguageViewModel
{
    public int FirmCount { get; set; }
    public ObservableCollection<FilterViewModel> Filters { get; set; } 
}

I have binding setup using the INotifyPropertyChanged, etc... and all is working. The final issue I have is with the Scrolling of the TabItem content in my first tab. The requirements call for only the tabs with overflowing content to scroll and not the entire tab control. I.e. - the tab headers should still be viewable including controls above the tab control itself and the scroll bars should appear insideof Tab1's TabItem area. I've played with this for hours to no avail. I'm obviously doing something wrong here and could use some assistance.

我使用 INotifyPropertyChanged 等进行了绑定设置……并且一切正常。我遇到的最后一个问题是在我的第一个选项卡中滚动 TabItem 内容。要求只需要滚动内容溢出的选项卡,而不是整个选项卡控件。即 - 选项卡标题应该仍然可见,包括选项卡控件本身上方的控件,并且滚动条应该出现Tab1 的 TabItem 区域内。我已经玩了几个小时无济于事。我显然在这里做错了,可以使用一些帮助。

A bit more detail: The binding on the CheckBox(es) / ItemControls on the Values collection can have upward of 200 - 500 controls and thus causes everything to get knocked out of wack.

更详细一点:Values 集合上的 CheckBox(es)/ItemControls 上的绑定可以有 200 - 500 个以上的控件,从而导致一切都变得古怪。

回答by pushpraj

you can make use of a Gridcontainer instead of StackPanel, I attempted to make a sample for you

您可以使用Grid容器代替StackPanel,我试图为您制作一个样本

sample

样本

        <ScrollViewer Name="ScrollViewer1"
                      HorizontalScrollBarVisibility="Auto"
                      VerticalScrollBarVisibility="Auto">
            <Grid Name="Tab1StackPanel">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <TextBlock FontSize="24"
                           FontWeight="Bold"
                           Foreground="DarkSlateGray"
                           FontStyle="Normal">
                    Filters
                </TextBlock>
                <Button x:Name="ClearFiltersButton"
                        Grid.Row="1"
                        Background="DarkRed"
                        Foreground="White"
                        FontSize="20"
                        FontWeight="Bold"
                        MaxWidth="124"
                        HorizontalAlignment="Left">
                    Clear Filters
                </Button>
                <StackPanel Orientation="Horizontal"
                            Grid.Row="2">
                    <TextBlock>
                        <Run>Total Paragraphs </Run><Run Text="{Binding ResearchLanguageViewModel.TotalCount}"></Run>
                    </TextBlock>
                </StackPanel>
                <ItemsControl ItemsSource="{Binding ResearchLanguageViewModel.Filters, Mode=OneWay}" 
                              Grid.Row="3">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding Path=Type}"></TextBlock>
                                <TextBox Text="Search...."></TextBox>
                                <ItemsControl ItemsSource="{Binding Path=Values, Mode=OneWay}">
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <CheckBox Content="{Binding Mode=OneWay}"></CheckBox>
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>
                            </StackPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </Grid>
        </ScrollViewer>

grid with row definitions of auto height behaves same like a stack panel except the last one which uses up the remaining space.

具有自动高度行定义的网格的行为与堆栈面板相同,除了最后一个用完剩余空间。

we can adjust it further to match the exact needs

我们可以进一步调整它以满足确切的需求