我有一个ListBox,它可以数据绑定到一个ObservableCollection,并且有一个复杂的ItemContainerStyle(带有图像,进度条和文本)
当我尝试在其CollectionView上应用过滤器时,该集合中有大约200个项目,UI将冻结2-3秒。仅当ListBox的高度未固定(即允许增长)时才会发生这种情况。将ListBox高度设置为值(例如500
)时即刻<Style x:Key="CollectionStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="Bd" VerticalAlignment="Center" BorderBrush="Transparent" BorderThickness="1" CornerRadius="3.5" Margin="2" Padding="2">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="{Binding Path=ListIcon}" Width="32" Margin="5,5,0,5" VerticalAlignment="Center" />
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Margin="10,0">
<TextBlock Text="{Binding Path=Name}" HorizontalAlignment="Stretch" VerticalAlignment="Center" Style="{StaticResource TextFontStyleTextBlock}" FontSize="14" TextTrimming="CharacterEllipsis" />
<TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="{Binding Path=Time,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}" Style="{StaticResource LabelFontStyleTextBlock}" TextTrimming="CharacterEllipsis" FontSize="12" Margin="5,0,0,0"/>
</StackPanel>
<ProgressBar Grid.Row="1" Height="10" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Margin="10,0,10,0" Value="{Binding Percentage, Mode=OneWay, Converter={StaticResource doubleToProgress}}">
<ProgressBar.Foreground>
<MultiBinding Converter="{StaticResource ProgressBarColorConverter}">
<Binding Mode="OneWay" Path="Percentage" />
<Binding Mode="OneWay" Path="IsStatic" />
</MultiBinding>
</ProgressBar.Foreground>
</ProgressBar>
<StackPanel Orientation="Horizontal" Grid.Row="2" Margin="10,0,10,0">
<Image VerticalAlignment="Center" Visibility="{Binding IsStatic, Converter={StaticResource CollapsedIfFalse}, Mode=OneWay, FallbackValue=Collapsed}" Source="/Common;component/Images/alert.ico" Margin="2,2,2,2"
RenderOptions.BitmapScalingMode="HighQuality"
RenderOptions.EdgeMode="Aliased" />
<TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="{Binding Path=Description,Mode=OneWay}" Foreground="Gray" TextTrimming="CharacterEllipsis" FontSize="11"/>
</StackPanel>
</Grid>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<ListBox Grid.Row="0" x:Name="items" ItemsSource="{Binding Path=MyCollection}"
ItemContainerStyle="{StaticResource CollectionStyle}" />
有没有办法解决这种冻结?在ListBox上具有固定高度是不可接受的结果,因为它导致垂直滚动条出现在已具有垂直滚动条的控件中
答案 0 :(得分:1)
在ListBox上具有固定高度是不可接受的结果,因为它会导致垂直滚动条出现在已有垂直滚动条的控件中
这看起来很可疑。当ListBox
的直接父级小组不是 Grid
或DockPanel
时(其最后一个小孩为ListBox
且LastChildFill=true
),列表框失去了它的虚拟化和滚动。由于虚拟化丢失,列表框会对所有行进行去虚拟化,同时这样做会挂起UI线程。
您的ListBox
是否包含在某种滚动查看器中?遗憾的是,如果是这种情况,您将不得不跳过滚动查看器或将固定大小应用于ListBox
。
如果您希望列表框为auto-occupy
剩余的容器空间,请通过某个转换器对父面板的实际高度ptoperty进行一些绑定,并为该转换器中的ListBox“计算”所需的Height
。这样看起来ListBox占用了所有空间,但实际上是通过绑定应用了一些固定高度。
但根据情况,这可能很棘手!