简体   繁体   English

ScrollBar在ScrollViewer内部不可见

[英]ScrollBar not visible inside ScrollViewer

I caught myself stuck in this problem for a while and I seem to be unable to manage it. 我陷入了这个问题一段时间,似乎无法解决。 I created a UserControl named TaskListControl that essentially is a list of another UserControl named TaskListItemControl and I want it to show the vertical ScrollBar when the content overflows but it doesn't happen. 我创建了一个名为TaskListControlUserControl ,它本质上是另一个名为TaskListItemControl UserControl的列表,并且我希望它在内容溢出但没有发生时显示垂直ScrollBar

After some searching and test I tried to break down the CustomControl because I suspect that the problem is related to the undefined space occupation by the items list. 经过一些搜索和测试后,我试图分解CustomControl因为我怀疑问题与项目列表中未定义的空间占用有关。 I included the ScrollViewer inside a Grid and placed it inside the MainWindow , but nothing changes. 我将ScrollViewer包含在Grid ,并将其放置在MainWindow ,但没有任何变化。

Here is the code for the TaskListItem contained inside the list: 这是列表中包含的TaskListItem的代码:

<UserControl x:Class="CSB.Tasks.TaskListItemControl"
         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:CSB.Tasks"
         xmlns:core="clr-namespace:CSB.Tasks.Core;assembly=CSB.Tasks.Core"
         mc:Ignorable="d"
         Height="70"
         d:DesignHeight="100" d:DesignWidth="400">

<!-- Custom control that represents a Task. -->
<UserControl.Resources>
    <!-- The control style. -->
    <Style x:Key="ContentStyle" TargetType="{x:Type ContentControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ContentControl}">

                    <Border x:Name="ContainerBorder" BorderBrush="{StaticResource LightVoidnessBrush}"
                            Background="{StaticResource DeepVoidnessBrush}"
                            BorderThickness="1" 
                            Margin="2">

                        <!-- The grid that contains the control. -->
                        <Grid Name="ContainerGrid" Background="Transparent">

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>

                            <!-- Border representing the priority state of the Task:
                            The color is defined by a ValueConverter according to the PriorityLevel of the Task object. -->
                            <Border Grid.Column="0"
                                    Width="10"
                                    Background="{Binding Priority, Converter={local:PriorityLevelToRGBConverter}}">
                            </Border>

                            <!-- Border containing the Task's informations. -->
                            <Border Grid.Column="1" Padding="5">
                                <StackPanel>
                                    <!-- The title of the Task. -->
                                    <TextBlock Text="{Binding Title}" FontSize="{StaticResource TaskListItemTitleFontSize}" Foreground="{StaticResource DirtyWhiteBrush}"/>

                                    <!-- The customer the Taks refers to. -->
                                    <TextBlock Text="{Binding Customer}" Style="{StaticResource TaskListItemControlCustomerTextBlockStyle}"/>

                                    <!-- The description of the Task. -->
                                    <TextBlock Text="{Binding Description}"
                                               TextTrimming="WordEllipsis"
                                               Foreground="{StaticResource DirtyWhiteBrush}"/>
                                </StackPanel>
                            </Border>

                            <!-- Border that contains the controls for the Task management. -->
                            <Border Grid.Column="2"
                                    Padding="5">

                                <!-- Selection checkbox of the Task. -->
                                <CheckBox Grid.Column="2" VerticalAlignment="Center"/>
                            </Border>

                        </Grid>

                    </Border>

                    <!-- Template triggers. -->
                    <ControlTemplate.Triggers>

                        <DataTrigger Binding="{Binding IsSelected}" Value="True">
                            <Setter Property="Background" TargetName="ContainerBorder" Value="{StaticResource VoidnessBrush}"/>
                            <Setter Property="BorderBrush" TargetName="ContainerBorder" Value="{StaticResource PeterriverBrush}"/>
                        </DataTrigger>

                        <EventTrigger RoutedEvent="MouseEnter">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Duration="0:0:0:0" To="{StaticResource LightVoidness}" Storyboard.TargetName="ContainerGrid" Storyboard.TargetProperty="Background.Color"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>

                        <EventTrigger RoutedEvent="MouseLeave">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Duration="0:0:0:0" To="Transparent" Storyboard.TargetName="ContainerGrid" Storyboard.TargetProperty="Background.Color"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<!-- Content of the control: assignment of the DataContext for design-time testing. -->
<ContentControl d:DataContext="{x:Static core:TaskListItemDesignModel.Instance}" 
                Style="{StaticResource ContentStyle}"/>

And here is the TaskListControl code: 这是TaskListControl代码:

<UserControl x:Class="CSB.Tasks.TaskListControl"
         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:CSB.Tasks"
         xmlns:core="clr-namespace:CSB.Tasks.Core;assembly=CSB.Tasks.Core"
         mc:Ignorable="d" 
         d:DesignHeight="500" d:DesignWidth="500">

<!-- Custom control that represents a list of TaskListItemControl. -->
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <ScrollViewer Grid.Row="0"
                  VerticalScrollBarVisibility="Auto"
                  HorizontalScrollBarVisibility="Auto"
                  DataContext="{x:Static core:TaskListDesignModel.Instance}"
                  Height="{Binding RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}, Path=Height}">

        <!-- The items shown in the list. -->
        <ItemsControl ItemsSource="{Binding Items}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <local:TaskListItemControl/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

    </ScrollViewer>

</Grid>

As you can see, I set the DataContext in order to test the controls and I can actually see the ScrollBar inside the design preview: 如您所见,我设置了DataContext以便测试控件,并且实际上可以在设计预览中看到ScrollBar

实际结果。

EDIT: I managed to show the ScrollBar but it seems to overflow the Window that contains the TaskListControl since I bound it's height to the Window height that, obviously, takes in account the titlebar too. 编辑:我设法显示ScrollBar但它似乎溢出包含TaskListControlWindow ,因为我将它的高度绑定到显然要考虑标题栏的Window高度。 Here is the code of the MainWindow where the control is used: 这是使用控件的MainWindow的代码:

<Window x:Class="CSB.Tasks.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:CSB.Tasks"
    mc:Ignorable="d"
    Title="{StaticResource MainWindow_TitleText}"
    Style="{StaticResource WindowDefaultStyle}"
    Height="500" 
    Width="500"
    WindowStartupLocation="CenterScreen">

<WindowChrome.WindowChrome>
    <WindowChrome ResizeBorderThickness="{Binding ResizeBorderThickness}"
                  GlassFrameThickness="0"
                  CornerRadius="{Binding CornerRadius}"/>
</WindowChrome.WindowChrome>

<local:TaskListControl>
    <local:TaskListControl/>
</local:TaskListControl>

The TaskListControl is placed directly in the Window since I tried to put it in almost every type of "container" ( Border , StackPanel , Grid , etc.) but no luck at all, the height still overflows. 由于我试图将TaskListControl放置在几乎每种类型的“容器”( BorderStackPanelGrid等)中,但是没有运气,高度仍然溢出,因此TaskListControl直接放置在Window

Since I'd like to handle the height directly in the UserControl definition avoiding to do so every time I use it: 由于我想直接在UserControl定义中处理高度,避免每次使用时都这样做:

  • Where should I place the TaskListControl inside the MainWindow (what type of container)? 我应该将TaskListControl放在MainWindow什么位置(哪种类型的容器)?
  • In which way should I set the height of the TaskListControl inside the UserControl definition (now it's bound to the Window height, but it's not correct at all)? 我应该以哪种方式在UserControl定义内设置TaskListControl的高度(现在它已绑定到Window的高度,但根本不正确)?

Here is the result of what I accomplished until now (you can see the bottom scroll button missing): 这是到目前为止我完成的工作的结果(您可以看到底部滚动按钮丢失了):

最后结果

Does anyone have any suggestions? 有没有人有什么建议? Thank you all in advance for the help. 预先感谢大家的帮助。

For debugging such problems, the best way is to set scrollbar visibility to Visible so you can see how your ScrollViewer grows. 为了调试此类问题,最好的方法是将滚动条可见性设置为Visible,以便您可以看到ScrollViewer的增长情况。 Probably it grows bigger than the screen size and if you set the scrollbar visibility to Visible, the bottom scroll button will be missing. 它可能会变得大于屏幕尺寸,并且如果将滚动条可见性设置为“可见”,则底部滚动按钮将丢失。

Maybe you have put your user control inside a container that has a height of * so it grows bigger than the screen and the scrollbar will never be shown. 也许您已将用户控件放在一个*高度的容器中,因此它变得比屏幕大,并且滚动条将永远不会显示。

Update : I have checked your project, I believe the problem is the Grid in Windows.xaml. 更新 :我已经检查了您的项目,我相信问题是Windows.xaml中的网格。 In rows 23-27 change the order of the last two RowDefinitions like this: 在第23-27行中,如下更改最后两个RowDefinition的顺序:

<Grid.RowDefinitions>
    <RowDefinition Height="{Binding TitleHeight}"/>
    <RowDefinition Height="*"/>
    <RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

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

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