水平列表框中的项目垂直布局

5

我有一个水平的ListBox。以下是它的代码(去掉了一些无关的内容):

            <ListBox Grid.Row="1"
                     ItemContainerStyle="{StaticResource ListBoxUnselectableItemStyle}"
                     ItemsSource="{Binding ...}"
                     BorderThickness="0"
                     Background="{x:Null}"
                     ScrollViewer.CanContentScroll="False">

                <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"
                                        VerticalAlignment="Top"
                                        HorizontalAlignment="Center"
                                        Background="Red"/>
                        </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
            </ListBox>

我得到的项目布局行为如下所示: http://www.freeimagehosting.net/uploads/cf4f839d02.png 正如您所看到的,第二个项目较小,并且VerticalLayout不在我所希望的顶部。
有人可以帮我吗?
1个回答

5

HorizontalAlignment和VerticalAlignment指的是StackPanel在其父容器中的对齐方式。

您可能需要查看ItemContainerStyle,其中您可以将VerticalAlignment设置为Stretch(以使ListBoxItem占据ListBox的整个高度),并将VerticalContentAlignment设置为Top(以使ListBoxItem的内容与ListBoxItem顶部对齐)。


这对我也起作用。对于像我这样的新手,样式为: <ListBox.ItemContainerStyle>     <Style TargetType="ListBoxItem">         <Setter Property="VerticalContentAlignment">             <Setter.Value>                 Top             </Setter.Value>         </Setter>     </Style> </ListBox.ItemContainerStyle> - Jobo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接