WPF ListView 总是显示完整的项目

标签 wpf listview scrollviewer

我有一个带有多个 ListView 控件的应用程序,其中要求 ListView 中的项目必须完全可见。列表中永远不应显示部分 ListViewItem。如果用户在最终显示部分项目的位置释放 ScrollViewer,则列表应“对齐”并自行更正,以便仅显示完整项目。

以前有人这样做过吗?我想我需要重载 ListView 和/或 ScrollViewer 才能做到这一点。我正在寻找有关如何处理此问题的建议。谢谢。

这是我的 list 之一:

<ctrls:SnapList x:Name="PART_ProductList" 
                                            ScrollViewer.CanContentScroll="False"  
                                            ScrollViewer.VerticalScrollBarVisibility="Auto" 
                                            ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                                            ItemContainerStyle="{StaticResource ProductFinderItem}" 
                                            Canvas.Top="373" Canvas.Left="75"
                                            Height="910" Width="900" >

                            <ctrls:SnapList.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <VirtualizingStackPanel VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Standard" />
                                </ItemsPanelTemplate>
                            </ctrls:SnapList.ItemsPanel>

                            <ctrls:SnapList.Template>
                                    <ControlTemplate>
                                    <ScrollViewer  x:Name="Scroller" VerticalAlignment="Top"  CanContentScroll="True" Style="{StaticResource VertScrollViewer}" Focusable="false" >
                                                <ItemsPresenter   />
                                            </ScrollViewer>
                                    </ControlTemplate>
                            </ctrls:SnapList.Template>
                        </ctrls:SnapList>

最佳答案

我相信您正在寻找 ScrollViewer.CanContentScroll属性(property)

如果您知道 ListView 中 item 的最大尺寸,请确保 ListView 的 Height 大小相同,或者是 item 高度的倍数。

工作示例:

<ListView ScrollViewer.CanContentScroll="True"
            Height="200">

    <ListBoxItem Height="50"
                    Content="Test1" />
    <ListBoxItem Height="50"
                    Content="Test2" />
    <ListBoxItem Height="50"
                    Content="Test3" />
    <ListBoxItem Height="50"
                    Content="Test4" />
    <ListBoxItem Height="50"
                    Content="Test5" />
    <ListBoxItem Height="50"
                    Content="Test6" />
    <ListBoxItem Height="50"
                    Content="Test7" />
    <ListBoxItem Height="50"
                    Content="Test8" />
    <ListBoxItem Height="50"
                    Content="Test9" />
    <ListBoxItem Height="50"
                    Content="Test10" />

</ListView>

Content in a ScrollViewer can be scrolled in terms of physical units or logical units. Physical units are device independent pixels. Logical units are used for scrolling items within an ItemsControl. The default behavior of the ScrollViewer is to use physical units to scroll its content. However, in cases where the CanContentScroll is set to true, the content could use logical units to scroll. For example, ListBox, ListView, and other controls that inherit from ItemsControl use logical units to scroll. If CanContentScroll is true, the values of the ExtentHeight, ScrollableHeight, ViewportHeight, and VerticalOffset properties are number of items, instead of physical units.

If you require physical scrolling instead of logical scrolling, wrap the host Panel element in a ScrollViewer and set its CanContentScroll property to false. Physical scrolling is the default scroll behavior for most Panel elements.

关于WPF ListView 总是显示完整的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4186001/

相关文章:

c# - 如何在本地网络的一台计算机上运行业务逻辑,在另一台计算机上运行GUI?

c# - 如何以编程方式使用 Windows 文件资源管理器执行搜索?

c# - 将 ListView 绑定(bind)到具有对象类型的 List<T> 或 List<InterfaceOfT>

wpf - 如何使 WPF DataGrid VerticalScrollBar 始终可见?

wpf - 如何制作 2 状态图像按钮?

java - 如何在将列表项添加到 ListView 之前以编程方式设置列表项中的字体大小?

android - 如何在 ListView 中绘制圆角矩形形状的项目

java - ListView 或 ScrollPane 滚动条不滚动

c# - 将新行添加到 XAML 中的 TextBlock 时,将 ScrollViewer 滚动到 WPF 中的底部

c# - ScrollViewer 鼠标滚轮不滚动