Ok i have an WPF application in wich i create my own ListBoxItem
in a resource dictionary that looks like this :
<DataTemplate x:Key="StationItem">
<Grid x:Name="Gridder" Tag="{Binding SItem.StationName}" Width="125" Height="55">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Rectangle x:Name="Rectagler" Width="120" Height="50" RadiusX="5" RadiusY="5" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.ColumnSpan="2" Grid.RowSpan="2" StrokeThickness="1" Stroke="Black">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFFF3A00" Offset="0"/>
<GradientStop Color="#FFE88D20" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlock Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" x:Name="StationName" Text="{Binding SItem.StationName}" Foreground="Black" FontSize="14" FontWeight="SemiBold" TextWrapping="NoWrap" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0,0,0"/>
<TextBlock Grid.Column="0" Grid.Row="1" x:Name="StationCountry" Text="{Binding SItem.StationCountry}" Foreground="WhiteSmoke" FontSize="11" FontWeight="Light" TextWrapping="NoWrap" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0,0,8"/>
<Image Tag="{Binding SItem.StationName}" Width="15" Height="15" RenderOptions.BitmapScalingMode="HighQuality" Grid.Column="1" Grid.Row="1" Source="{Binding FavSource}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,10,10"/>
</Grid>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Rectagler" Property="Width" Value="125"/>
<Setter TargetName="Rectagler" Property="Height" Value="55"/>
<Setter TargetName="StationName" Property="Foreground" Value="White"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
A ListBox
like this :
<ListBox x:Name="StationsListLB" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="5" ItemTemplate="{DynamicResource GenreStyle}" ItemContainerStyle="{DynamicResource ContainerStyle}" Background="{x:Null}" BorderBrush="{x:Null}" PreviewMouseLeftButtonDown="StationsListLB_PreviewMouseLeftButtonDown" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" PreviewMouseDoubleClick="StationsListLB_PreviewMouseDoubleClick">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Vertical" MaxHeight="220" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
And i populate my ListBox
items when the user chooses an Genre of the station, with a method like this :
public class Stations
{
public StationItem SItem { get; set; }
public string FavSource { get; set; }
}
public class StationItem
{
public string StationName { get; set; }
public string StationUrl { get; set; }
public string StationGenre { get; set; }
public string StationWebsite { get; set; }
public string StationCountry { get; set; }
}
private void LoadStationByGenre(string Genre)
{
StationsListLB.Items.Clear();
StationsListLB.ItemTemplate = (DataTemplate)FindResource("StationItem");
IEnumerable<Stations> results = StationList.Where(s => s.SItem.StationGenre.Equals(Genre));
foreach (var Station in results)
{
StationsListLB.Items.Add(Station);
}
}
Note : Every StationGenre will populate my ListBox
with at least 1000 StationItems
.
My code works fine and starts with a memory of 40MB (Window Task Manager). But whenever i select a genre and populate my ListBox
, memory explodes at 100MB.And i then select another genre memory explodes further. I thought that with ListBox.Items.Clear();
the ListBoxItems
will be destroyed from memory too, but this doesnt happen.
I tried to remove the image from my ListoBoxItemTemplate
and checked my app's memory, but didnt see a real difference (About 4-7 MB's difference).
So my answer is : How can i make my application consume a lot less memory when adding items in the ListBox
?
Thanks in advance!!