2015-11-19 234 views
0

我有一個列表框,其中包含一些元素。C#WPF垂直列表框不顯示垂直滾動條

<StackPanel Orientation="Vertical" Grid.Row="0" > 
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"> 
     <TextBlock x:Name="lbGroups" Text="PartPrograms Groups" FontSize="{StaticResource TEXTBOX_TITLE_FONTSIZE}" FontWeight="Bold" Margin="20" HorizontalAlignment="Left" VerticalAlignment="Center" TextAlignment="Center" Grid.Row="1"/> 
     <Button x:Name="btAddGroup" Content="&#xF055;" FontSize="{StaticResource TEXTBOX_BIGBUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Click="Button_Click"/> 
     <Button Name="btDeleteGroup" Content="&#xF057;" FontSize="{StaticResource TEXTBOX_BIGBUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Click="Button_Click"/> 
     <Button x:Name="btGroupDown" Content="&#xF0AB;" FontSize="{StaticResource TEXTBOX_BIGBUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Click="Button_Click"/> 
     <Button Name="btGroupUp" Content="&#xF0AA;" FontSize="{StaticResource TEXTBOX_BIGBUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Click="Button_Click"/> 
    </StackPanel> 
    <ListBox Name="lbPPgroups" Background="{x:Null}" Margin="0" ScrollViewer.VerticalScrollBarVisibility="Visible"> 
    </ListBox> <------- this is the listbox 
</StackPanel> 

元素以編程方式添加到列表框與此:

void AddNewPartProgramGroup(String strContent, String strNotes, String strPathImage, bool IsChecked=false) 
      { 
        StackPanel sp = new StackPanel(); 
        string currentDir = AppDomain.CurrentDomain.BaseDirectory.ToString(); 
        ToggleButton toggleButton = new ToggleButton() 
        { 
          Content = strContent, 
          Height = IMAGES_ROW_HEIGHT/GOLDEN_RATIO, 
          Width = IMAGES_ROW_HEIGHT, 
          FontSize = 10, 
          Background = null, 
          Tag = "bt" + strContent, 
          ToolTip = strNotes, 
          Margin = new Thickness(BUTTON_MARGIN), 
          IsChecked = IsChecked 
        }; 
        toggleButton.Click += new RoutedEventHandler(ToggleButton_Click); 

        sp.Children.Add(toggleButton); 
        Image newResizedImage = ImageUtilities.StrPath2ResizedImageSizeHeight(strPathImage, IMAGES_ROW_HEIGHT); 
        sp.Children.Add(newResizedImage); 
        sp.Orientation = Orientation.Horizontal; 
        sp.HorizontalAlignment = HorizontalAlignment.Left; 

        this.lbPPgroups.Items.Add(sp);<------ here I add elements 

        var newGroup = new PcDmisData.Group(); 
        newGroup.Description = strContent; 
        var newImage = new PcDmisData.MyImage(); 
        newImage.Image = newResizedImage; 
        newImage.IsImageEmbedded = false; 

        newGroup.myImage = newImage; 
        newGroup.Notes = strNotes; 
        EasyRunData.lstPPgroups.Add(newGroup); 
      } 

問題增加了一些元素後,我無法看到列表框的垂直滾動條: enter image description here

我也嘗試添加一個垂直滾動查看器,但沒有奏效。 Thanx的任何幫助 PAtrick

+0

檢查StackPanel的父面板的寬度。它不應該大於窗口的寬度。 – tgpdyk

回答

1

所以問題是,外部StackPanel沒有真正的MaxHeight和高度自動更新。 ScrollBar僅在該面板高度達到特定限制時出現。要解決這個問題,你可以玩MaxHeight ...

我會推薦使用DockPanel。

<Grid> 
    <DockPanel Grid.Row="0" > 
     <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" HorizontalAlignment="Left"> 
      <TextBlock x:Name="lbGroups" Text="PartPrograms Groups" FontWeight="Bold" Margin="20" HorizontalAlignment="Left" VerticalAlignment="Center" TextAlignment="Center"/> 
      <Button Name="btGroupUp" Click="btGroupUp_Click" Margin="2,2,2,2" Width="30"/> 
     </StackPanel> 
     <ListBox Name="lbPPgroups" Margin="0" ScrollViewer.VerticalScrollBarVisibility="Auto"/> 
    </DockPanel> 
</Grid> 

只是爲了後面的代碼示例:

private void btGroupUp_Click(object sender, RoutedEventArgs e) 
    { 
     for (var i=1;i<50;i++) 
     { 
      TextBox box = new TextBox(); 
      box.Text = "Hello World " + i ; 
      lbPPgroups.Items.Add(box); 
     } 
    } 

在這個例子中我設置ScrollViewer.VerticalScrollBarVisibility="Auto"這樣滾動條在需要的時候纔會出現。但是您也可以將其設置爲"Visible"

+0

非常感謝。我只需要設置它的高度和滾動條出現! – Patrick