我有一个带有多个 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/