【问题标题】:How to adjust control to fit to the screen inside a listbox?如何调整控件以适应列表框内的屏幕?
【发布时间】:2011-11-22 10:23:02
【问题描述】:

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

代码:

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

【问题讨论】:

    标签: windows-phone-7 listbox wrappanel listboxitems listbox-control


    【解决方案1】:

    尝试将以下容器样式添加到您的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

    更新:删除了 Jared Bienz 提议的 HorizontalAlignment

    【讨论】:

    • 从技术上讲,您只需要以下内容:ItemContainerStyle>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多