WPF Listview项目对齐方式

时间:2013-06-18 08:08:14

标签: wpf listview

我是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>

代码来自这个问题:

Arranging ListView items Horizontally

2 个答案:

答案 0 :(得分:2)

使用WrapPanel具有固定大小或ListViewItem的大小。

答案 1 :(得分:1)

怎么样的包裹?

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