WPF列表框,图像在内存中

时间:2016-09-30 10:52:30

标签: c# wpf xaml listbox

我正在使用图像列表框,它增加了千兆字节的内存使用量。我正在使用这个xaml。

 <Grid Grid.Row="0" Grid.Column="1">
            <ScrollViewer>
                <ListBox   ItemsSource="{Binding Path=FilterImportSlideCollection}" ItemTemplate="{StaticResource slideItemTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
            </ScrollViewer>
        </Grid>

模板是

 <DataTemplate x:Key="slideItemTemplate" >
    <Grid Width="100" Height="130"  Margin="2 2 2 2" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <Grid.RowDefinitions>
            <RowDefinition Height="80"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Border Background="White" BorderBrush="Black" BorderThickness="1" Grid.Row="0" Panel.ZIndex="3" Canvas.Left="0" Canvas.Right="0">
            <Image MinWidth="100" MinHeight="80" HorizontalAlignment="Center" Source="{Binding ImagePath, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></Image>
            </Border>
        <TextBlock HorizontalAlignment="Left" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding Title}" Foreground="Black"/>
    </Grid>
</DataTemplate>

每个ImagePath都有像这样的图像路径 &#34; https://localhost:2673/SlideThumbnail/1272.png&#34;

1 个答案:

答案 0 :(得分:1)

ListBox置于ScrollViewer内会禁用 UI虚拟化,这意味着您的所有ListItems都会立即在内存中创建。

尝试:

<Grid Grid.Row="0" Grid.Column="1">
    <ListBox ItemsSource="{Binding Path=FilterImportSlideCollection}" ItemTemplate="{StaticResource slideItemTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
</Grid>

有关虚拟化的更多信息,请查看MSDN:

Displaying Large Data Sets