水平ListBox项垂直布局

lev*_*ovd 5 wpf layout xaml listbox

我有水平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>
Run Code Online (Sandbox Code Playgroud)




我得到像这样的项目布局行为: http://www.freeimagehosting.net/uploads/cf4f839d02.png]

如您所见,第二项较小,VerticalLayout不是我想要的Top.

有谁能够帮我?

ito*_*son 5

Horizo​​ntalAlignment和VerticalAlignment指的是StackPanel在其父容器中的对齐方式.

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