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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/359217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:52:21  来源:igfitidea点击:

How do I make WPF ListView items repeat horizontally, like a horizontal scrollbar?

wpfxamllistviewalignment

提问by Ryan O'Neill

I have a WPF ListView which repeats the data vertically. I cannot figure out how to make it repeat horizontally, like the slideshow view in Windows Explorer. My current ListView definition is:

我有一个 WPF ListView,它垂直重复数据。我不知道如何让它水平重复,就像 Windows 资源管理器中的幻灯片视图一样。我当前的 ListView 定义是:

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

The DataTemplate is (although I believe this should not matter);

DataTemplate 是(尽管我认为这无关紧要);

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

回答by Boyan

Set the ItemsPanel of the ListView to a horizontal StackPanel. Like this:

将 ListView 的 ItemsPanel 设置为水平 StackPanel。像这样:

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

回答by Nate Noonen

Perhaps a better way to do this would be to use a VirtualizingStackPanel which has all of the same properties but is much more performant especially for listboxes with lots of items.

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

回答by stenly

I found it easier to go this way

我发现走这条路更容易

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