拉伸stackpanel

时间:2012-05-17 16:58:01

标签: windows-phone-7 xaml

我想使用列表框创建歌曲列表,但我遇到了布局问题。

目前我使用以下xaml:

<ListBox x:Name="lstHistory" HorizontalAlignment="Stretch" Margin="12,284,0,90" Width="460">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
                        <StackPanel Orientation="Horizontal">
                            <Image Margin="5" VerticalAlignment="top" Source="{Binding Image}" />
                        </StackPanel>
                        <StackPanel Orientation="Vertical" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                            <TextBlock Margin="8"
                                  TextWrapping="Wrap"
                                  VerticalAlignment="Top"
                                  HorizontalAlignment="Left"
                                  Text="{Binding Name}" />
                            <TextBlock Margin="8"
                                  VerticalAlignment="Top"
                                  HorizontalAlignment="Left"
                                  Text="{Binding Artist}" />
                            <TextBlock Margin="8,0,8,8"
                                  VerticalAlignment="Bottom"
                                  HorizontalAlignment="Right" Text="{Binding Time}" Opacity="0.25" />
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

我想把时间放在列表项的最右下角,但每次都在屏幕中间结束。

这是我得到的一个例子: enter image description here

谁能告诉我如何在那里抽出时间?

2 个答案:

答案 0 :(得分:5)

默认情况下,ListBoxItem占用尽可能少的空间。 StackPanel也是如此。为了使它具有跨度,您必须将ListBox的Horizo​​ntalContentAlignment设置为Stretch,并将StackPanel的Horizo​​ntalAlignment设置为Stretch(您已经在进行)。您还需要为ListBox设置ItemContainerStyle

<ListBox x:Name="lstHistory" HorizontalContentAlignment="Stretch" Margin="12,284,0,90">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
                    <Image Margin="5" VerticalAlignment="top" Source="{Binding Image}" />
                    <StackPanel Orientation="Vertical" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                        <TextBlock Margin="8"
                              TextWrapping="Wrap"
                              VerticalAlignment="Top"
                              HorizontalAlignment="Left"
                              Text="{Binding Name}" />
                        <TextBlock Margin="8"
                              VerticalAlignment="Top"
                              HorizontalAlignment="Left"
                              Text="{Binding Artist}" />
                        <TextBlock Margin="8,0,8,8"
                              VerticalAlignment="Bottom"
                              HorizontalAlignment="Right" Text="{Binding Time}" Opacity="0.25" />
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>

答案 1 :(得分:0)

HorizontalAlignment="Stretch"无法在StackPanel上运行。你应该设置一个明确的宽度(可能是432)。

此外,Image不需要单独位于StackPanel中。