簡體   English   中英

ListBox不顯示滾動條

[英]ListBox not showing scrollbar

我有一個帶有各種元素的列表框。 它位於具有列定義的網格內,但是當元素超出窗口時,必須具有滾動條,以便我可以看到全部內容。

在此處輸入圖片說明

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

我已經讀了很多的解決方案是這樣一個和我輕松地測試了一切准備:

  • 它在網格中。
  • 網格的列定義為*
  • 我添加了一個scrollviewer

但是沒有任何效果。

項目清單

我如何看待它,有兩個選擇,在這兩個中都需要“限制”包含的網格:

  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創建一個“超級網格”,其中包含包含RowDefinitionsListBoxItems (“子網格”),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> 

我會嘗試將列表框的寬度或最大寬度設置為某個值,例如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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM