2017-06-22 165 views
0

我有一個視圖,應該顯示信息並測試它我添加了testdata。但是,如果我爲屏幕空間添加太多項目,垂直滾動條不會出現。ListBox垂直滾動條不顯示(WPF)

這是我的XML代碼:

<UserControl x:Class="not relevant" 
    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" 
    xmlns:local="not relevant" 
    mc:Ignorable="d" 
    Width="250"> 

<Border Background="{DynamicResource BG}" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1"> 
    <StackPanel> 
     <Grid> 
      <Label Height="30" Content="Geöffnete Designs" HorizontalContentAlignment="Center"></Label> 
      <Separator Background="DarkGray"/> 
      <ListBox Background="{DynamicResource BG}" BorderThickness="0" VerticalAlignment="Stretch" > 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
      </ListBox> 
     </Grid> 
    </StackPanel> 
</Border> 

我嘗試添加一個滾動瀏覽器代替,但沒有正常工作。我做錯了什麼,或者我可能會遇到什麼問題?

回答

2

只需在您的xaml代碼中刪除StackPanel即可。你不需要它,只會產生問題。我建議改變你的造型佈局,因爲你不應該像你應該使用Grid

至少增加一些RowDefinitions佈局你的用戶界面,當你想顯示Label在頂部和下LabelList

可能看起來像這樣

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <Label Grid.Row="0" 
      Height="30" 
      Content="Geöffnete Designs" 
      HorizontalContentAlignment="Center" /> 
    <Separator Grid.Row="1" 
       Background="DarkGray" /> 
    <ListBox Grid.Row="2" 
      BorderThickness="0" 
      VerticalAlignment="Stretch"> 

Result

+0

很新的WPF中添加滾動瀏覽器。感謝您在編輯中的進一步解釋。應該可以閱讀一本書,而不是僅僅顯示代碼的教程而無需進一步解釋 – fbueckle

+0

歡迎您,我們都開始了一次:)祝您好運,學習WPF –

0

就在您的XAML

<ScrollViewer> 
    <Border BorderThickness="1"> 
     <StackPanel> 
      <Grid> 
       <Label Height="30" Content="Geöffnete Designs" HorizontalContentAlignment="Center"></Label> 
       <Separator Background="DarkGray"/> 
       <ListBox BorderThickness="0" VerticalAlignment="Stretch" > 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
       </ListBox> 
      </Grid> 
     </StackPanel> 
    </Border> 
</ScrollViewer> 
+0

看看我的答案。只需刪除'StackPanel'並使用像你應該使用的'Grid'並且你不必添加滾動查看器;) –

+0

是的,正如@Mighty Badaboom所建議的那樣,ScrollViewers在StackPanels中不能很好地工作。這是因爲StackPanel用無限的空間來度量它的子節點:https://stackoverflow.com/questions/41140287/horizo​​ntal-scroll-for-stackpanel-doesnt-work/41140885#41140885 – mm8