列表框不显示滚动条

ListBox not showing scrollbar

我有一个列表框,里面有各种元素。 它位于具有列定义的网格内,但是当元素超过 window 时,必须有滚动条以便我可以看到全部内容。

而 xaml 是:

<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <ListBox x:Name="lbxOptionsTab3" Background="{x:Null}" BorderBrush="Gainsboro" 
                 SelectionChanged="ListBox_SelectionChanged"
                 HorizontalContentAlignment="Stretch" Margin="10" 
                 ScrollViewer.HorizontalScrollBarVisibility="Visible"  
                 ScrollViewer.CanContentScroll="True">
            <ListBox.Effect>
                <DropShadowEffect ShadowDepth="4" Direction="330" Color="Black" Opacity="0.5" BlurRadius="4"/>
            </ListBox.Effect>
        </ListBox>
        <Border x:Name="Border2Tab3" BorderBrush="Gainsboro" 
                Background="{x:Null}"  MinWidth="100" 
                BorderThickness="5" Grid.Column="1" Margin="10,10,10,10">
            ...

我已经阅读了很多这样的解决方案one 简而言之,我已经测试了所有的可能性:

但没有任何效果。

列表项

我会尝试将列表框的宽度或最大宽度设置为某个值,例如 100。如果出现滚动条,则问题是列表框的宽度没有限制。所以他根据需要扩展,因此根本不需要显示它的滚动条。

查看列定义

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

自动占用您需要的所有空间
尝试:

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

样本

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="2*" />
    </Grid.ColumnDefinitions>
    <ListBox Grid.Row="0" Grid.Column="0" 
        ScrollViewer.HorizontalScrollBarVisibility="Visible" >
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
        <ListBoxItem Content="ppp this need to be long longer longest" />
    </ListBox>
    <Border Grid.Row="0" Grid.Column="1" BorderThickness="5" Margin="10" BorderBrush="Red"/>
</Grid>

我怎么看你有两个选择,你都需要 "restrained" 包含网格:

  1. 正如其他答案中所建议的那样,设置包含的网格宽度或最大宽度,只有当 ListBoxItems 高度大于网格高度时才会显示滚动条:

    <Grid Height="50">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
         </Grid.ColumnDefinitions>
        <ListBox x:Name="lbxOptionsTab3" Background="{x:Null}" BorderBrush="Gainsboro" SelectionChanged="ListBox_SelectionChanged" HorizontalContentAlignment="Stretch" Margin="10" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True">
            <ListBox.Effect>
                <DropShadowEffect ShadowDepth="4" Direction="330" Color="Black" Opacity="0.5" BlurRadius="4"/>
            </ListBox.Effect>
        </ListBox>
        <Border x:Name="Border2Tab3" BorderBrush="Gainsboro" Background="{x:Null}"  MinWidth="100" BorderThickness="5" Grid.Column="1" Margin="10,10,10,10"  >
    </Grid>
    
  2. 创建一个 "Super grid" with RowDefinitions 包含 ListBoxItems 包含网格 ("Sub grid"),RowDefinitions 将约束子网格(在例如,不超过 1/3 window 高度):

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
    
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
    
           <ListBox x:Name="lbxOptionsTab3"  ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True">
                <ListBoxItem Content="ppp" />
                <ListBoxItem Content="ppp" />
                <ListBoxItem Content="ppp" />
                <ListBoxItem Content="ppp" />
                <ListBoxItem Content="ppp" />
                <ListBoxItem Content="ppp" />
                <ListBoxItem Content="ppp" />
                <ListBoxItem Content="ppp" />
                <ListBoxItem Content="ppp" />
                <ListBoxItem Content="ppp" />
                <ListBoxItem Content="ppp" />
            </ListBox>
         </Grid>
    </Grid>