WPF列表视图项对齐

3

我对WPF还比较陌生。

我想知道如何让项目并排但仍然水平对齐。

现在这些是我的项目:

( 0 ) ( 0 ) ( 0 ) ( 0 ) ( 0 ) 

但是如果没有更多的空间,它将只显示一个滚动条,而不是像这样继续到下一行:

( 0 ) ( 0 ) ( 0 )
( 0 ) ( 0 )

这是我的 XAML 代码:
<ListView x:Name="listViewResourceHours" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0" SelectionMode="Single" Height="100" Width="500" >
    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.Panel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </GroupStyle.Panel>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock FontWeight="Bold" Text="{Binding Name, StringFormat={}{0}:}" />
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="GroupItem">
                                <StackPanel Orientation="Horizontal">
                                    <ContentPresenter Margin="0,0,0,0" VerticalAlignment="Center" />
                                    <ItemsPresenter Margin="0,0,0,0" VerticalAlignment="Center"/>
                                </StackPanel>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </ListView.GroupStyle>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" >
                <Label VerticalAlignment="Center"  Margin="0" Content="{Binding Hours}" />
                <Label Name="lblWorkingHours" VerticalAlignment="Center"  Margin="0,0,0,0" Content="{Binding WorkingHoursType, Converter={StaticResource ResourceKey=hoursTypeConverter}}" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>

</ListView>

这段代码来自于以下问题:

如何横向排列ListView中的项

2个回答

2

使用固定大小或与您的ListViewItem相同大小的WrapPanel


<ListView.ItemTemplate> <DataTemplate> <WrapPanel Orientation="Vertical" Width="90"> <Label VerticalAlignment="Center" Margin="0" Content="{Binding Hours}" /> <Label Name="lblWorkingHours" VerticalAlignment="Center" Margin="0,0,0,0" Content="{Binding WorkingHoursType, Converter={StaticResource ResourceKey=hoursTypeConverter}}" /> </WrapPanel> </DataTemplate> </ListView.ItemTemplate>好像不起作用 :( - MMM
当我将方向更改为垂直时,它仍然无法正常工作。 - MMM
它不会换行到下一行。 - MMM
尝试将WrapPanel放置在<ItemsPanelTemplate>内部。 - Johannes Wanzek
仍然不起作用,当我选择一个项目时,我可以看到项目的高度填满了整个区域,而不是只创建一个小框。 - MMM
啊,好吧,应该看到那个问题,是我的错! - Johannes Wanzek

1

什么是wrappanel?

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

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