C#WPF垂直列表框没有显示垂直滚动条

时间:2015-11-19 18:43:58

标签: c# wpf listbox scrollbar

我有一个包含一些元素的listBox。

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

元素以编程方式添加到listBox中:

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任何帮助 帕特里克

1 个答案:

答案 0 :(得分:1)

所以问题是外部StackPanel没有真正的MaxHeight,而Height会自动更新。仅当此面板达到其高度的某个限制时,才会显示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",因此仅在需要时才会显示ScrollBar。但您也可以将其设置为"Visible"