【问题标题】:WPF ListView Always show complete itemsWPF ListView 始终显示完整的项目
【发布时间】:2011-05-10 07:36:53
【问题描述】:

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

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

这是我的清单之一:

<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?您是否在包含多个 ListViews 的 Grid(或其他东西)中有一个 ScrollViewer,当该 ScrollViewer 更改位置时,您想确保所有 ListViewItems 都是可见的?对于 ListView 本身,我什至无法让它显示部分项目。
  • 我说的是列表的 ControlTemplate 中的 ScrollViewer。我将其中一个列表的 xaml 添加到我的帖子中。我所说的 ScrollViewer 是示例 XAML 中带有 x:Name="Scroller" 的那个。
  • ListBox 默认具有完全相同的行为。你能用 ListBox 代替 ListView 吗?

标签: wpf listview scrollviewer


【解决方案1】:

你试过ListView.ScrollIntoView吗?

【讨论】:

  • ScrollIntoView 会将一个项目滚动到视图中,但它不会确保只有整个项目在列表中可见。即使是这样,我仍然需要知道何时调用它(例如,显示部分项目时)。
  • 您当前的问题比较清楚。以前,它更像是您希望您的选择完全可见。相反,您根本不希望列表中显示任何部分项目。对吗?
  • 对。我的问题与我最初发布的问题相同。 :)
【解决方案2】:

您可以尝试跟踪滚动视图的当前滚动,然后获取所有项目的高度。然后您将知道如何计算每个项目的正确偏移量。当他们将滚动动画释放到最近的偏移量时。

ScrollInfo to get the current scroll offset and info.

Scroll to vertical offset

【讨论】:

    【解决方案3】:

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

    如果你知道 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>
    

    ScrollViewer 中的内容可以按物理单位滚动 或逻辑单元。物理单位是与设备无关的像素。 逻辑单元用于在 ItemsControl 中滚动项目。这 ScrollViewer 的默认行为是使用物理单位 滚动其内容。但是,如果 CanContentScroll 是 设置为 true,内容可以使用逻辑单位滚动。为了 例如,ListBox、ListView 和其他继承自的控件 ItemsControl 使用逻辑单位进行滚动。如果 CanContentScroll 为真, ExtentHeight、ScrollableHeight、ViewportHeight 和 VerticalOffset 属性是项目数,而不是物理 单位。

    如果您需要物理滚动而不是逻辑滚动,请换行 ScrollViewer 中的主机 Panel 元素并设置其 CanContentScroll 属性为假。物理滚动是默认滚动行为 对于大多数 Panel 元素。

    【讨论】:

      猜你喜欢
      • 2012-09-04
      • 1970-01-01
      • 2010-11-12
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多