gpt4 book ai didi

.net - VirtualizingStackPanel 未正确处理折叠的项目

转载 作者:行者123 更新时间:2023-12-01 09:35:18 26 4
gpt4 key购买 nike

我在 wpf 中有一个 ListView 用于显示字段列表;基于属性值,某些字段可以在运行时折叠。它工作正常,除了 ListVIew 不会折叠为 ListViewItem 保留的空间,这些空间在运行时被折叠了。

enter image description here

我能够在 Snoop 中看到额外的 ListViewItems(具有折叠的可见性),ListView 也会向上移动项目,但它不会调整其高度以删除空白空间!

我可以肯定地说,这是由于 VirtualizedStackPanel 而发生的,因为将 ItemsPanel 更改为 StackPanel 可以解决问题。这是相关的 ListView XAML:

<ListView
x:Class="Wizards.FieldBinderModelListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Margin="0"
VerticalAlignment="Top"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Top"
Background="White"
BorderThickness="0"
Grid.IsSharedSizeScope="True"
KeyboardNavigation.DirectionalNavigation="Continue"
Padding="1"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
SelectionChanged="ListViewSelectionChanged"
SelectionMode="Single">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<!--Works fine with StackPanel but not with VirtualizingStackPanel
Explicitly added this PanelTemplate to show that it works with
StackPanel;ListView uses VirtualizingStackPanel as default panel
and causes same problem-->
<!--<StackPanel Orientation="Vertical" VerticalAlignment="Top"/>-->
<VirtualizingStackPanel Orientation="Vertical"
VerticalAlignment="Top"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Black" />
</Trigger>
<DataTrigger Binding="{Binding Status}"
Value="{x:Static local:Status.NotExisting}">
<!--Hide the fields which are in NotExisting state;
Need a trigger here as Status can be different -->
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate DataType="{x:Type View:FieldViewModel}">
<local:FieldEditor
Margin="0,2,0,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Top"
Padding="0">
<!--<local:FieldEditor.Style>
<Style TargetType="{x:Type local:FieldEditor}">
<Style.Triggers>
<DataTrigger
Binding="{Binding Status}"
Value="{x:Static local:Status.NotExisting}">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</local:FieldEditor.Style>-->
</local:FieldEditor>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

这是 VirtualizingStackPanel 中的错误吗?还有其他人遇到过类似的问题吗?有什么解决方法吗?


更新:

在连接时将此错误报告给 MS - https://connect.microsoft.com/VisualStudio/feedback/details/734113/virtualizingstackpanel-not-handling-collapsed-items-correctly

最佳答案

我设法重现了您的问题。它看起来确实像 VirtualizingStackPanel 中的一个错误。一种解决方法是将隐藏项的高度设置为零而不是折叠它们:

<DataTrigger Binding="{Binding Status}" Value="False">
<Setter Property="Height" Value="0" />
<Setter Property="IsEnabled" Value="False"/>
<!--<Setter Property="Visibility" Value="Collapsed" />-->
</DataTrigger>

关于.net - VirtualizingStackPanel 未正确处理折叠的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9191701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com