简体   繁体   English

WPF:使 ListBox 成为父级的全高,但也可以滚动

[英]WPF: Make ListBox full height of parent, but also be scrollable

I have a simple app that has three columns in the window.我有一个简单的应用程序,窗口中有三列。 In the middle column, I have a ListBox component.在中间列中,我有一个 ListBox 组件。

What I want is the ListBox to stretch the full height of the column, but also be scrollable when it has a lot of stuff in it.我想要的是 ListBox 可以拉伸列的整个高度,而且当它有很多东西时也可以滚动。 Right now, it's not scrollable.目前,它不可滚动。 I can fix this by adding a Height (eg Height="300" ) property to it, but then it won't stretch with the column anymore.我可以通过向它添加一个Height (例如Height="300" )属性来解决这个问题,但它不会再随着列拉伸。 What do I do?我该怎么办?

<Window x:Class="UI.MainWindow"
    <!-- window stuff -->
<Border Padding="10">
    <StackPanel>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" MinWidth="150" MaxWidth="200" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <!-- column 1 stuff -->

            <StackPanel x:Name="LogLines" Grid.Column="1">
                <StackPanel Orientation="Horizontal">
                    <Label>Search</Label>
                    <TextBox x:Name="Searchbox" Height="20" TextWrapping="Wrap" Text="TextBox" Width="120" />
                    <CheckBox x:Name="ErrorsOnlyCheckbox" HorizontalAlignment="Right">
                        Errors only
                    </CheckBox>
                </StackPanel>

                <ListBox
                    x:Name="LogLinesList"
                    ScrollViewer.CanContentScroll="True" />
            </StackPanel>

            <GridSplitter Grid.Column="1" Width="10" />

            <!-- column 3 stuff -->
        </Grid>
    </StackPanel>
</Border>
</Window>

在此处输入图片说明

Container of your ListBox is StackPanel . ListBox容器是StackPanel StackPanel will increase regarding child controls. StackPanel将增加关于子控件。 You should use another container, try to use Grid instead您应该使用另一个容器,尝试使用Grid代替

<Grid x:Name="LogLines" Grid.Column="1">
     <Grid.RowDefinitions>
         <RowDefinition Width="Auto"/>
         <RowDefinition Width="*" />
     </Grid.RowDefinitions>
    
     <StackPanel Orientation="Horizontal" Grid.Row="0">
          ....
     </StackPanel>
    
     <ListBox Grid.Row="1"/>
</Grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM