如何制作可滚动、可调整大小的列表视图?

问题描述 投票:0回答:1

本来我想让listview依赖于窗口高度参数,但老实说我觉得这个解决方案不切实际,相信它可以做得更好。

所以,这是我感兴趣的对象:

<StackPanel>
            <TextBlock Text="{Binding LessonTitle}" Foreground="Black" HorizontalAlignment="Center" Margin="0,10,0,0" FontSize="36" Background="DimGray"></TextBlock>
            <Grid Height="Auto" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Hidden">
                <ListView x:Name="LV2" Background="DimGray"
                          BorderBrush="Transparent"
                          ItemsSource="{Binding Lessons}"
                       ScrollViewer.VerticalScrollBarVisibility="Hidden" 
                      ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                      SizeChanged="LV2_SizeChanged">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Height="1000"
                                        ScrollViewer.CanContentScroll="True" 
                                        ScrollViewer.VerticalScrollBarVisibility="Hidden">
                                    <TextBlock Width="Auto" FontSize="24" Foreground="Black" TextWrapping="Wrap">
                                        <Run Text="{Binding LessonText}"/>
                                    </TextBlock>
                                    <Image Source="{Binding LessonImage}"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
            </Grid>
        </StackPanel>

我的列表视图和几次尝试使其可滚动(也可随窗口调整大小)的残余,可滚动我的意思是使整个列表平滑滚动,而不是每个元素滚动,但我不知道如何实现它。

我已经尝试将堆栈面板更改为停靠面板/网格,反之亦然,不同的 scrollviwer 位置,但它们都不起作用。到目前为止,我最多只能滚动列表视图的第一项或逐项滚动。

c# wpf xaml listview scrollviewer
1个回答
0
投票
<StackPanel>
            <TextBlock Text="{Binding LessonTitle}" Foreground="Black" HorizontalAlignment="Center" Margin="0,10,0,0" FontSize="36" Background="DimGray"></TextBlock>
            <Grid Height="Auto" >
                <ListBox x:Name="LV2" Width="400" Height="400" Background="DimGray"
                          BorderBrush="Transparent"
                          ItemsSource="{Binding Lessons}"
                      SizeChanged="LV2_SizeChanged">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Height="1000">
                                    <TextBlock FontSize="24" Foreground="Black" TextWrapping="Wrap">
                                        <Run Text="{Binding LessonText}"/>
                                    </TextBlock>
                                    <Image Source="{Binding LessonImage}"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
            </Grid>
        </StackPanel>

ListBox 默认有这些属性。只需定义可见区域的大小,超出该范围的所有内容都可以滚动。 您可能希望将项目分隔到它们自己的项目槽中,而不是扩展到远远超出其内容尺寸的堆叠面板。 这是一个例子:

<StackPanel>
    <TextBlock Text="{Binding LessonTitle}" Foreground="Black" HorizontalAlignment="Center" Margin="0,10,0,0" FontSize="36" Background="DimGray"></TextBlock>
    <Grid Height="504" >
        <ListBox x:Name="LV2" Height="400" Background="DimGray" BorderBrush="Transparent" ItemsSource="{Binding Lessons}" Panel.ZIndex="1" VerticalContentAlignment="Stretch" ScrollViewer.CanContentScroll="True">
                <TextBlock FontSize="24" Foreground="Black" TextWrapping="Wrap" Text="{Binding LessonText}" />
                <Image Source="/Image2.png" Width="297" Height="498"/>
        </ListBox>
    </Grid>
</StackPanel>

如果您需要用大量项目填充列表,最简单的方法是在 .cs / vb 代码隐藏中。 C# 会是这样的:

var lessons = //the source of the items.
foreach(var item in lessons){ LV2.Items.Add((new Grid/Canvas/Panel{ Child/Children.Add/Content = item }));

如果您需要多种类型的父控件,请添加一个 if else 循环来检查类型和一个 foreach 循环来处理相应的类型

© www.soinside.com 2019 - 2024. All rights reserved.