从左到右排列WPF ListItems

时间:2016-06-07 16:29:37

标签: c# wpf listbox

我试图让列表项从左到右堆叠,而不是从上到下堆叠。

<ListBox x:Name="listBox1" HorizontalAlignment="Stretch" Height="454" Margin="10,10,0,0" VerticalAlignment="Top" Width="943">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="BorderThickness" Value="3.0"/>
            <Setter Property="Width" Value="Auto" />
            <Setter Property="HorizontalAlignment" Value="Stretch"/>   
            <Setter Property="Margin" Value="3,3,3,3"/>
            <Setter Property="FontSize" Value="16"/>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel HorizontalAlignment="Right" FlowDirection="LeftToRight">
                <Grid>
                    <TextBlock Text="Machine Name:" />
                </Grid>
                <Grid>
                    <TextBlock Text="{Binding Mname}" />
                </Grid>
                <Grid>
                    <TextBlock Text="{Binding PartName}" />
                </Grid>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我似乎只能将它们叠加在一起。我改变的任何对齐似乎通常都没有效果。

我尝试用Grid围绕StackPanel包裹FlowDirection="LeftToRight" HorizontalAlignment="Stretch",但我只能将列表项直接放在窗口的中心,或者左边叠在一起。

2 个答案:

答案 0 :(得分:2)

您想要Orientation="Horizontal"

StackPanel.Orientation property

答案 1 :(得分:2)

<ListBox
    ...
    >
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel
                Orientation="Horizontal"
                />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>