简体   繁体   English

滚动以查看WPF TabItems(不是整个TabControl)

[英]Scrolling for WPF TabItems (not entire TabControl)

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. 我使用INotifyPropertyChanged等设置绑定设置,并且一切正常。 The final issue I have is with the Scrolling of the TabItem content in my first tab. 我遇到的最后一个问题是第一个选项卡中TabItem内容的滚动。 The requirements call for only the tabs with overflowing content to scroll and not the entire tab control. 需求只要求滚动具有溢出内容的选项卡,而不是整个选项卡控件。 Ie - the tab headers should still be viewable including controls above the tab control itself and the scroll bars should appear inside of Tab1's TabItem area. 即-选项卡标题仍应可见,包括选项卡控件上方的控件,并且滚动条应出现 Tab1的TabItem区域内。 I've played with this for hours to no avail. 我已经玩了好几个小时都没有用。 I'm obviously doing something wrong here and could use some assistance. 我显然在这里做错了,可以使用一些帮助。

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(s)/ ItemControls上的绑定最多可以包含200-500个控件,从而使所有内容消失w尽。

you can make use of a Grid container 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 我们可以进一步调整以满足实际需求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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