0

我正在尝试使用包装在包装面板中的图像创建列表框。我想显示适合屏幕的项目。比如如果项目宽度小于可用屏幕宽度,那么项目应该被拉伸,如果项目宽度大于可用屏幕宽度,那么项目应该跳转到下一行并且应该填充空白空间。

代码:

    <ListBox x:Name="lst" Grid.Row="3"   HorizontalAlignment="Left">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <toolkit:WrapPanel Width="Auto" Height="Auto" ite ItemWidth="Auto" ItemHeight="Auto"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

        <Border BorderThickness="2" Width="Auto" Margin="5,5,5,5" Height="Auto"  BorderBrush="Cornsilk">
            <StackPanel Orientation="Vertical" Height="150" Width="150">

                <Image Source="Images/image-s.png" Height="120" Width="120" ></Image>
                <TextBlock Text="Item" HorizontalAlignment="Center"></TextBlock>
            </StackPanel>

        </Border>

        <Border BorderThickness="2"  Margin="5,5,5,5" Width="Auto" Height="Auto"  BorderBrush="Cornsilk">
            <StackPanel Orientation="Vertical" Height="150" Width="150">

                <Image Source="Images/image-s.png" Height="120" Width="120" ></Image>
                <TextBlock Text="Item" HorizontalAlignment="Center"></TextBlock>
            </StackPanel>
        </Border>
        <Border BorderThickness="2"  Margin="5,5,5,5" Width="Auto" Height="Auto"  BorderBrush="Cornsilk">
            <StackPanel Orientation="Vertical" Height="150" Width="150">

                <Image Source="Images/image-s.png" Height="120" Width="120" ></Image>
                <TextBlock Text="Item" HorizontalAlignment="Center"></TextBlock>
            </StackPanel>
        </Border>
        <Border BorderThickness="2"  Margin="5,5,5,5"  Width="Auto" Height="Auto"  BorderBrush="Cornsilk">
            <StackPanel Orientation="Vertical" Height="150" Width="150">

                <Image Source="Images/image-s.png" Height="120" Width="120" ></Image>
                <TextBlock Text="Item" HorizontalAlignment="Center"></TextBlock>
            </StackPanel>
        </Border>
    </ListBox>
4

1 回答 1

2

尝试将以下容器样式添加到您的ListBox:

<UserControl x:Class="MyClass"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <UserControl.Resources>
        <Style x:Key="myContainerStyle" TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </UserControl.Resources>

    <ListBox ItemContainerStyle="{StaticResource myContainerStyle}">

    </ListBox>
</UserControl>

(最终你必须使用PhoneApplicationPage代替UserControl

更新:HorizontalAlignment按照 Jared Bienz 的建议删除

于 2011-11-22T12:54:58.897 回答