如何使WPF ListView项水平重复,就像水平滚动条一样?

80

我有一个WPF ListView,它垂直重复显示数据。我无法想出如何使其水平重复,就像Windows资源管理器中的幻灯片视图一样。我的当前ListView定义是:

我有一个 WPF 的 ListView,在垂直方向上重复展示数据。我无法找到方法让它在水平方向上重复展示,就像 Windows Explorer 中的幻灯片视图一样。我当前的 ListView 定义如下:

<ListView ItemsSource="{StaticResource MyDataList}" ItemTemplate="{StaticResource ListViewTemplate}">
</ListView>

数据模板是(尽管我相信这并不重要);

                <Rectangle HorizontalAlignment="Stretch" Margin="0,1,0,0" x:Name="rectReflection" Width="Auto" Grid.Row="1" Height="30">
                    <Rectangle.Fill>
                        <VisualBrush Stretch="None" AlignmentX="Center" AlignmentY="Top" Visual="{Binding ElementName=imgPhoto}">
                            <VisualBrush.RelativeTransform>
                                <TransformGroup>
                                    <MatrixTransform Matrix="1,0,0,-1,0,0" />
                                    <TranslateTransform Y="1" />
                                </TransformGroup>
                            </VisualBrush.RelativeTransform>
                        </VisualBrush>
                    </Rectangle.Fill>
                    <Rectangle.OpacityMask>
                        <RadialGradientBrush GradientOrigin="0.5,1.041">
                            <RadialGradientBrush.RelativeTransform>
                                <TransformGroup>
                                    <ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1.202" ScaleY="2.865"/>
                                    <SkewTransform AngleX="0" AngleY="0" CenterX="0.5" CenterY="0.5"/>
                                    <RotateTransform Angle="0" CenterX="0.5" CenterY="0.5"/>
                                    <TranslateTransform X="-0.002" Y="-0.491"/>
                                </TransformGroup>
                            </RadialGradientBrush.RelativeTransform>
                            <GradientStop Color="#D9000000" Offset="0"/>
                            <GradientStop Color="#01FFFFFF" Offset="0.8"/>
                        </RadialGradientBrush>
                    </Rectangle.OpacityMask>
                </Rectangle>
            </Grid>
        </Border>
    </DataTemplate>
3个回答

218

将ListView的ItemsPanel设置为水平的StackPanel,如下所示:

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

12

也许更好的方法是使用VirtualizingStackPanel,它具有所有相同的属性,但对于具有大量项目的列表框而言,它的性能要更高。


9

我发现这样做更加容易

<ItemsControl ItemsSource="{Binding Path=Steps}">
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding PageName}" Padding="10" />
    </DataTemplate>
</ItemsControl.ItemTemplate>    
<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel></WrapPanel>
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>


1
但是WrapPanel有时会换行吗? - El Mac
直到它到达右边框并从左到右作为新行继续。 - st35ly

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