当窗口变小时,ListBox内容不会调整大小

时间:2012-03-31 05:26:06

标签: wpf xaml

我正在使用.NET 4.0(不是.NET 4.0 CP),并遇到了这个独特的问题。我创建了一个ListBox来显示绑定元素,这里首先是我的XAML的一部分。

<Grid Grid.Row="2" Background="#EEEEEE">
        <Border Margin="6,10,10,10" BorderBrush="#666666" BorderThickness="1">
            <ListBox ItemsSource="{Binding}" Name="appList" BorderThickness="0" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="80" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Border Grid.Column="0" Margin="5" BorderThickness="3" CornerRadius="2" BorderBrush="Black" HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="ItemBorder">
                                <Image Width="64" Height="64"  Source="{Binding Path=IconUri}" Stretch="UniformToFill" />
                            </Border>
                            <StackPanel Margin="0,5,5,5" Grid.Column="1" Orientation="Vertical" HorizontalAlignment="Stretch">
                                <TextBlock FontSize="18" Text="{Binding Path=DisplayName}" />
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="60"/>
                                    </Grid.ColumnDefinitions>
                                    <ProgressBar Grid.Column="0" Height="24" HorizontalAlignment="Stretch" IsIndeterminate="{Binding Path=IsDiscovering}" Value="{Binding Path=PercentageDownloaded}" />
                                    <TextBlock Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"><TextBlock x:Name="percentageDownloaded" /><TextBlock x:Name="percentageMeter">%</TextBlock></TextBlock>
                                </Grid>
                            </StackPanel>
                        </Grid>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Path=IsDiscovering}">
                                <DataTrigger.Value>True</DataTrigger.Value>
                                <Setter TargetName="percentageDownloaded" Property="Text" Value="N/A" />
                                <Setter TargetName="percentageMeter" Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=IsDiscovering}">
                                <DataTrigger.Value>False</DataTrigger.Value>
                                <Setter TargetName="percentageDownloaded" Property="Text" Value="{Binding Path=PercentageDownloaded}" />
                                <Setter TargetName="percentageMeter" Property="Visibility" Value="Visible" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ListBox>
        </Border>
    </Grid>

调整窗口大小可以很好地拉伸ListBox内容,但是当我调整它的大小时,它会保留它的宽度并生成垂直滚动条。

1 个答案:

答案 0 :(得分:2)

您是否已尝试将StackPanel用作ItemsPanel?

    <ListBox.ItemsPanel>
       <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>