【问题标题】:ListBox not showing scrollbar列表框不显示滚动条
【发布时间】:2016-07-25 12:39:20
【问题描述】:

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

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

  • 它在一个网格中。
  • 网格的列定义为 *
  • 我添加了一个滚动查看器

但没有任何效果。

列表项

【问题讨论】:

    标签: c# wpf listbox scrollbar


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      查看列定义

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

      【讨论】:

        【解决方案3】:

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

        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. 使用包含ListBoxItemsRowDefinitions 创建一个“超级网格”(“子网格”),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> 
          

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-01-22
          • 2016-01-08
          • 2019-10-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-13
          相关资源
          最近更新 更多