WPF 列表视图垂直滚动条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31658655/
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
WPF listview vertical scrollbar
提问by user3101007
I've got a listview control with a gridview as it's view property. One of it's columns is dedicated to display a really long text. That column cell template is set to a TextBlock. Whenever listview item source contains only one item, no matter that mentioned TextBlock content starts to exceed listview height, vertical scroll is unavailable.
我有一个带有 gridview 的列表视图控件,因为它是视图属性。其中一栏专用于显示非常长的文本。该列单元格模板设置为 TextBlock。每当列表视图项源仅包含一项时,无论提到的 TextBlock 内容开始超过列表视图高度,垂直滚动都不可用。
<ListView ItemsSource="{Binding Path=ReportEntries}" VerticalContentAlignment="Top" ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="VerticalContentAlignment" Value="Top"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Path}" />
<GridViewColumn Header="Message" Width="50" DisplayMemberBinding="{Binding Message}" />
<GridViewColumn Header="Details" DisplayMemberBinding="{Binding Details}" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
回答by almulo
Set ScrollViewer.CanContentScroll="False"on your ListView.
设置ScrollViewer.CanContentScroll="False"在您的ListView.
<ListView ItemsSource="{Binding Path=ReportEntries}"
VerticalContentAlignment="Top"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.CanContentScroll="False">
<!-- etc. -->
</ListView>
The ListViewis trying to virtualize, by default. Virtualizing means, among other things, using logical scroll. Logical scrolling means the ScrollViewer scrolls item by item, jumping from one item to another instead of smoothly scrolling through the whole extent of each item.
在ListView尝试虚拟化,在默认情况下。除其他外,虚拟化意味着使用逻辑滚动。逻辑滚动意味着 ScrollViewer 逐项滚动,从一个项目跳到另一个项目,而不是平滑滚动每个项目的整个范围。
By setting CanContentScrollto false, you're telling the ScrollViewerto stop using logical scrolling, which also deactivates virtualization.
通过设置CanContentScroll为false,您告诉ScrollViewer停止使用逻辑滚动,这也会停用虚拟化。
If you're gonna show LOTS of items in that ListView, maybe virtualization is a big deal and this will introduce a performance issue.
如果你要在其中展示很多项目ListView,也许虚拟化是一个大问题,这会带来性能问题。
If you're just gonna show a handful of items, then this should be completely fine.
如果你只是想展示一些项目,那么这应该是完全没问题的。
EDIT- If performance is indeed an issue and you need to keep virtualization activated, consider setting a fixed height for all rows and adding its own ScrollVieweraround the TextBlockwith the big text.
编辑- 如果性能确实是一个问题并且您需要保持激活虚拟化,请考虑为所有行设置固定高度并在大文本ScrollViewer周围添加自己的高度TextBlock。
回答by Abin
Try adding a scrollViewer in DataTemplate like below,
尝试在 DataTemplate 中添加一个滚动查看器,如下所示,
<DataTemplate>
<ScrollViewer>
<TextBlock TextWrapping="Wrap" Text="{Binding}"/>
</ScrollViewer>
</DataTemplate>


