ListBox未显示滚动条

时间:2016-04-05 14:02:26

标签: c# wpf listbox scrollbar

我有一个包含各种元素的列表框。 它位于具有列定义的网格内,但是当元素超出窗口时,必须有滚动条,以便我可以看到整个内容。

enter image description here

和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 简而言之,我已经测试了所有可能性:

  • 它在网格中。
  • 网格的列定义为*
  • 我添加了一个scrollviewer

但没有任何效果。

列出项目

3 个答案:

答案 0 :(得分:1)

我怎么看你有两个选择,你需要“限制”包含的网格:

  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. 使用RowDefinitions创建一个包含ListBoxItems网格(“子网格”)的“超级网格”,RowDefinitions将限制子网格(在示例中,不是更多) 1/3窗口高度):

    <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> 
    

答案 1 :(得分:0)

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

答案 2 :(得分:0)

查看列定义

<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>