Hello devs,
Currently I am working on a project that display a list of item inside wrappanel, and each item's template will contain a TextBlock which display the item's name and a text label with localization.
The problem is, the list_1 is 1000 items long, it take around 1 second for first load, which is fine, however, when I switch the warppanel to load the list_2 (1000 items long), it will take 2 second, and switch the warppanel to load back the list_1, it will take 5 seconds, and each time when I switch the ItemsSource, it will take longer time to load the list.
But for the same logic, without localization, the time for loading is constant (around 1 second) for loading the list, no matter how many times you switch the ItemsSource.
Does anyone has this problem before? Please help.. Thanks!
Sample project download link:
[http://www.verge.hk/temp/michael/localizeproject.zip](http://www.verge.hk/temp/michael/localizeproject.zip)
MainWindow.xaml
```
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Engine="clr-namespace:WPFLocalizeExtension.Engine;assembly=WPFLocalizeExtension"
xmlns:lex="http://wpflocalizeextension.codeplex.com"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="LocalizeProject.MainWindow"
lex:LocalizeDictionary.DesignCulture="en"
Title="MainWindow" Height="600" Width="800">
<Window.Resources>
<Style x:Key="ListViewItemStyle1" TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Grid Height="70" HorizontalAlignment="Left" Width="70">
<TextBlock HorizontalAlignment="Stretch" Margin="0,0,0,34" TextWrapping="Wrap" Text="{Binding ItemName}" Width="Auto" Height="Auto"/>
<Grid Height="34" VerticalAlignment="Bottom" Background="Red" Margin="4">
<TextBlock x:Name="SoldoutLabel" Margin="0,8" TextWrapping="Wrap" Text="{lex:LocText LocalizeProject:MultiLanguage:WPF_沽清}" Foreground="White"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
<WrapPanel Orientation="Horizontal"
Width="{Binding ActualWidth,RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" ScrollViewer.VerticalScrollBarVisibility="Disabled" d:DesignWidth="386" />
</ItemsPanelTemplate>
<Style x:Key="ListViewItemStyle2" TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Grid Height="70" HorizontalAlignment="Left" Width="70">
<TextBlock HorizontalAlignment="Stretch" Margin="0,0,0,34" TextWrapping="Wrap" Text="{Binding ItemName}" Width="Auto" Height="Auto"/>
<Grid Height="34" VerticalAlignment="Bottom" Background="Red" Margin="4">
<TextBlock Margin="0,8" TextWrapping="Wrap" Text="Soldout" Foreground="White"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ItemsPanelTemplate x:Key="ItemsPanelTemplate2">
<VirtualizingStackPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
<DataTemplate x:Key="DataTemplate1">
<Grid Height="20" HorizontalAlignment="Left" Width="20">
<TextBlock HorizontalAlignment="Stretch" Margin="0,0,0,5" TextWrapping="Wrap" Text="{Binding ItemName}" Width="Auto" Height="Auto"/>
<Grid Height="10" VerticalAlignment="Bottom" Background="Red" Margin="4">
<TextBlock Margin="0" TextWrapping="Wrap" Text="{lex:LocText LocalizeProject:MultiLanguage:a01}" Foreground="White"/>
</Grid>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid d:DataContext="{d:DesignData /SampleData/MainWindowViewModelSampleData.xaml}">
<ListView x:Name="ListViewLeft" HorizontalAlignment="Left" Margin="8,50,0,80" ItemsSource="{Binding AllItemMasterList}" Width="275" ItemContainerStyle="{DynamicResource ListViewItemStyle1}" ItemsPanel="{DynamicResource ItemsPanelTemplate1}">
<ListView.View>
<GridView>
<GridViewColumn/>
</GridView>
</ListView.View>
</ListView>
<ListView x:Name="ListViewRight" Margin="287,50,228,80" ItemsSource="{Binding AllItemMasterList_1}" ItemContainerStyle="{DynamicResource ListViewItemStyle2}" ItemsPanel="{DynamicResource ItemsPanelTemplate1}">
<ListView.View>
<GridView>
<GridViewColumn/>
</GridView>
</ListView.View>
</ListView>
<Button x:Name="LeftPageUpButton" Content="List 1" HorizontalAlignment="Left" Height="40" Margin="8,0,0,36" VerticalAlignment="Bottom" Width="105" Click="LeftPageUpButton_Click"/>
<Button x:Name="LeftPageDownButton" Content="List 2" HorizontalAlignment="Left" Height="40" Margin="117,0,0,36" VerticalAlignment="Bottom" Width="105" Click="LeftPageDownButton_Click"/>
<Button x:Name="RightPageUpButton" Content="List 1" Height="40" Margin="287,0,0,36" VerticalAlignment="Bottom" Click="RightPageUpButton_Click" HorizontalAlignment="Left" Width="105"/>
<Button x:Name="RightPageDownButton" Content="List 2" Height="40" Margin="0,0,283,36" VerticalAlignment="Bottom" Click="RightPageDownButton_Click" HorizontalAlignment="Right" Width="105"/>
<TextBlock HorizontalAlignment="Left" Height="38" Margin="8,8,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="214"><Run Language="zh-tw" Text="With Localization"/></TextBlock>
<TextBlock Height="38" Margin="287,8,283,0" TextWrapping="Wrap" VerticalAlignment="Top"><Run Language="zh-tw" Text="Without Localization"/></TextBlock>
</Grid>
</Window>
```
__Updates 31/01/2014:__
Moreover, the output windows will display the follow error whenever I feed a new list to the itemsource:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.FrameworkElement', AncestorLevel='1''. BindingExpression:Path=Parent; DataItem=null; target element is 'MainWindow' (Name=''); target property is 'Parent' (type 'DependencyObject')
Comments: Hi,
I fixed the Data errors in the latest GitHub version. Please refer to #9277 for a copy of this revision DLL.
Regarding your problem, there is still one major issue, that I cannot solve from my side:
You create a lot of extension instances (1k each list) that are bound to the controls. After flipping the list, these controls wait for being garbage collected. Until this moment, the corresponding extension instances will remain active. And as they cannot see the special state of their bound controls, they continue working and firing events etc. before the GC comes along removing both, the control and the extension instance. But this takes some time at 1k instances. This, in turn, means (choose one):
- you have to trigger GC on your own (not good)
- you have to destroy the controls and the bond to the particular extensions on your own (better)
- you could simply get rid of the whole mess and fetch this particular string from a centralized point (e.g. your window). Then you'll only have one permanent extension instance for this one string. (best)
- you have a far more better idea that is worth sharing here (better than best :-))