C# WPF:在高度属性设置为“自动”的情况下,防止 ListView 中的水平滚动条覆盖最后一项
我在 StackPanel 中有一个 WPF ListView,高度=“自动”。很高兴它实现了我想要的 - 根据 ListView 中包含的所有项目更改其高度。我的 ListView 上还有 HorizontalScrollBarVisibity="Auto" ,这适合我的需要。
当我的 ListView 列的整体宽度大于 ListView 控件的宽度(即用户减小窗口和该控件的宽度)时,就会出现问题。在这种情况下,显然会出现水平滚动条 - 没关系。问题是这个滚动条覆盖了 ListView 项目的最后一个(底部)项目,因此它部分不可见。
垂直滚动条没有出现,这没关系 - 我不想要它。我只想正确计算 ListView 高度,因此它会考虑显示的水平滚动条的高度并将其包含在计算中,以便所有项目都完全可见。
Xaml 代码示例:
<ListView Name="lvProcedures" ItemsSource="{Binding Path=Procedures}" SelectionMode="Single">
<ListView.View>
<GridView AllowsColumnReorder="False" ColumnHeaderToolTip="Lista zabiegów">
<GridViewColumn DisplayMemberBinding="{Binding Path=procedure}" Header="A" Width="150" />
<GridViewColumn DisplayMemberBinding="{Binding Path=location}" Header="B" Width="100" />
<GridViewColumn DisplayMemberBinding="{Binding Path=material}" Header="C" Width="180" />
<GridViewColumn DisplayMemberBinding="{Binding Path=other}" Header="D" Width="180" />
<GridViewColumn DisplayMemberBinding="{Binding Path=description}" Header="E" Width="400" />
</GridView>
</ListView.View>
</ListView>
我现在一直坚持创建用户控件(也许只有 ListView 的派生类??),它覆盖 MeasureOverride 但我不认为这是解决如此小但令人恼火的“错误”的好方法,而且它也仍然让我在实施解决方案时遇到麻烦。
有人可以为这个问题提供一些优雅的解决方案吗? 任何想法将不胜感激。
I have a WPF ListView inside StackPanel, with Height="Auto". It's great that it does what I wanted to - changes it's height according to the all items contained in ListView. I have also HorizontalScrollBarVisiblity="Auto" on my ListView, which suits my needs.
The problem occurs when the whole width of columns of my ListView is greater than width of ListView control (i.e. user decreases width of a window and this control). In that case obviously horizontal scrollbar appears - thats ok. The problem is that this scrollbar covers last (bottom) item of the ListView's items so it is partially not visible.
Vertical scrollbar doesn't appear and this is ok - I don't want it. I just want to have my ListView height beeing calculated properly, so it considers the height of shown horizontal scrollbar and includes it in calculation so all items are wholy visible.
Xaml code example:
<ListView Name="lvProcedures" ItemsSource="{Binding Path=Procedures}" SelectionMode="Single">
<ListView.View>
<GridView AllowsColumnReorder="False" ColumnHeaderToolTip="Lista zabiegów">
<GridViewColumn DisplayMemberBinding="{Binding Path=procedure}" Header="A" Width="150" />
<GridViewColumn DisplayMemberBinding="{Binding Path=location}" Header="B" Width="100" />
<GridViewColumn DisplayMemberBinding="{Binding Path=material}" Header="C" Width="180" />
<GridViewColumn DisplayMemberBinding="{Binding Path=other}" Header="D" Width="180" />
<GridViewColumn DisplayMemberBinding="{Binding Path=description}" Header="E" Width="400" />
</GridView>
</ListView.View>
</ListView>
I've now hanged on idea to create user control (maybe only ListView's derieved class??) which overrides MeasureOverride but I don't think it is good way to solve such a small but iritating "bug" and it also still makes me trouble to implement the solution.
Can somebody provide some elegant solution for this problem?
Any idea would be appreciated.
@bartivo:我没有得到与你相同的结果 - 底部水平滚动条似乎没有遮盖最低的项目。我想知道您是否为 ListView 和 ScrollViewer 使用自定义 ControlTemplate,b/c 默认 ScrollViewer 模板有两行,第一行中的 ScrollContentPresenter (Height="*") 和第二行中的 HorizontalScrollBar (Height= “自动”)从而确保一个不会与另一个重叠。
也许您可以尝试对 ListView 和 ScrollViewer 使用这些样式,看看该问题是否仍然出现:
@bartivo: I'm not getting the same result as you - the bottom horizontal scrollbar doesn't appear to obscure the lowest item. I'm wondering if you're using a custom ControlTemplate for your ListView and ScrollViewer, b/c the default ScrollViewer template has two rows the ScrollContentPresenter in the first (Height="*") and the HorizontalScrollBar in the 2nd row (Height="Auto") thus ensuring that the one doesn't overlap the other.
Maybe you could try using these styles for your ListView and ScrollViewer and see if that problem still occurs: