【发布时间】:2010-09-16 00:33:03
【问题描述】:
我在开发照片查看器应用程序时遇到了一个问题。 我使用 ListBox 来显示图像,它包含在 ObservableCollection 中。 我将 ListBox 的 ItemsSource 绑定到 ObservableCollection。
<DataTemplate DataType="{x:Type modeldata:ImageInfo}">
<Image
Margin="6"
Source="{Binding Thumbnail}"
Width="{Binding ZoomBarWidth.Width, Source={StaticResource zoombarmanager}}"
Height="{Binding ZoomBarWidth.Width, Source={StaticResource zoombarmanager}}"/>
</DataTemplate>
<Grid DataContext="{StaticResource imageinfolder}">
<ScrollViewer
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Disabled">
<ListBox Name="PhotosListBox"
IsSynchronizedWithCurrentItem="True"
Style="{StaticResource PhotoListBoxStyle}"
Margin="5"
SelectionMode="Extended"
ItemsSource="{Binding}"
/>
</ScrollViewer>
我还将 ListBox 中的 Image'height 与滑块绑定。(滑块的值也绑定到 zoombarmanager.ZoomBarWidth.Width)。 但是我发现如果集合变得更大,例如:包含超过 1000 张图像,如果我使用滑块更改 iamges 的大小,它会变得有点慢。 我的问题是。 1. 为什么会变慢?成为它尝试缩放每个图像,或者它只是因为 notify("Width") 被调用超过 1000 次。 2. 有什么方法可以解决这种问题,让它更快。
PhotoListBoxStyle 是这样的:
<Style~~ TargetType="{x:Type ListBox}" x:Key="PhotoListBoxStyle">
<Setter Property="Foreground" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}" >
<WrapPanel
Margin="5"
IsItemsHost="True"
Orientation="Horizontal"
VerticalAlignment="Top"
HorizontalAlignment="Stretch" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style~~>
但是如果我使用上面的样式,我必须在 ListBox 之外使用 ScrollViewer,否则我不知道如何获得平滑滚动的滚动条,并且包装面板似乎没有默认滚动条。有人帮忙吗?据说带有滚动查看器的列表框性能很差。
【问题讨论】:
-
绑定每个图像的高度/宽度是非常低效的,只需在 ItemsPanel 上使用 LayoutTransform(对此效果添加了答案)。
标签: wpf image listbox performance