代码之家  ›  专栏  ›  技术社区  ›  Ralph Shillington

为什么这个列表框没有垂直滚动条

  •  0
  • Ralph Shillington  · 技术社区  · 14 年前

    <UserControl x:Class="SimpleStack.MainPage"
        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"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
        <UserControl.Resources>
            <DataTemplate x:Key="ListBoxItemTemplate">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Place holder"/><TextBlock Text="{Binding}"/>
                </StackPanel>
            </DataTemplate>
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="Azure">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="The Text" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"/>
            <ListBox ItemsSource="{Binding ListOfNumbers}" Grid.Row="1" Grid.Column="0"
                     ItemTemplate="{StaticResource ListBoxItemTemplate}"/>
            <TextBlock Text="Place Holder" Grid.Row="1" Grid.Column="1"/>
        </Grid>
    </UserControl>
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   Philip Rieck    14 年前

    这个 * 行高

        2
  •  1
  •   Dan Auclair    14 年前

    我的理解是,由于measure/arrange布局系统,您实际上是在告诉ListBox它可以拥有所需的所有垂直空间,而不受约束。因此,默认ListBox模板中的内部ScrollViewer永远不会被约束为触发滚动条显示。

    我可以找到两种方法来解决你的问题:

    -指定 ScrollViewer.VerticalScrollBarVisibility="Visible"

    -使用实际的ScrollViewer来包含ListBox,并让它提供滚动功能,而不是内部ListBox中的滚动功能(您可能需要调整填充和边框以使其看起来正确):

    <ScrollViewer Grid.Row="1" Grid.Column="0">
        <ListBox ItemsSource="{Binding ListOfNumbers}"
                 ItemTemplate="{StaticResource ListBoxItemTemplate}"/>
    </ScrollViewer>
    

    我更喜欢第二种方式,因为它只会显示垂直滚动条,如果真的有必要的话。