0

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

目前我使用以下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>

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

这是我得到的一个例子: 在此处输入图像描述

谁能告诉我怎么去那里的时间?

4

2 回答 2

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>
于 2012-05-17T19:24:03.770 回答
0

HorizontalAlignment="Stretch"不适用于 StackPanel。您应该设置一个明确的宽度(可能是 432)。

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

于 2012-05-17T18:57:25.017 回答