Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to render usercontrols separately
    primarykey
    data
    text
    <p>I have a UserControl drawing the datacontext with Itemscontrol etc. I'm having three objects of the same usercontrol but with different datacontexts which is initialized on startup of the program. The problem is that the datacontext is so big so it's drawing about 2000 UIelements on each of the objects. This takes so much time and what really is annoying me is that WPF is running in a single UIThread, so my computer is using 1/8 of the total CPU power to draw this. How can I make this faster?</p> <p>The code (DynamicShelViewModel is a usercontrol)</p> <pre><code>dataContextBestSellers = new DynamicShelfViewModel(_config, dataRepository, ProductSelectionMode.BestSellers); dataContextNormal = new DynamicShelfViewModel(_config, dataRepository, ProductSelectionMode.Normal); dataContextFull = new DynamicShelfViewModel(_config, dataRepository, ProductSelectionMode.AllProducts); ScrollGrid = new Grid(); ScrollGrid.Children.Add(dataContextBestSellers); ScrollGrid.Children.Add(dataContextNormal); ScrollGrid.Children.Add(dataContextFull); </code></pre> <p>This takes about 2 minutes to accomplish.</p> <p>This is the XAML code of DynamicShelfViewModel</p> <pre><code>&lt;Grid&gt; &lt;ItemsControl x:Name="ShelvesControl" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" DataContext="{Binding}" Width="{Binding Size.Width}" VerticalAlignment="Top" Height="{Binding Size.Height}" Grid.Column="0" Grid.Row="1" ItemsSource="{Binding ShelvesInViewPort}"&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;StackPanel VerticalAlignment="Stretch " HorizontalAlignment="Stretch" Orientation="Vertical" /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Grid Name="ShelfGrid" Height="{Binding Height}" MaxHeight="{Binding Height}" DataContext="{Binding}"&gt; &lt;Grid.Background&gt; &lt;SolidColorBrush Color="{Binding Color}" /&gt; &lt;/Grid.Background&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="*" /&gt; &lt;RowDefinition Height="{Binding SplitterSize}" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBlock Name="stretchingLabel" Height="{Binding SplitterSize}" Padding="0" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"&gt; &lt;TextBlock.Background&gt; &lt;ImageBrush ImageSource="{Binding Converter={Utilities1:FancySourceConverter}, ConverterParameter=Images/ShelvesImages/shelfhorisontal.png}" /&gt; &lt;/TextBlock.Background&gt; &lt;Grid VerticalAlignment="Stretch" Width="{Binding ElementName=stretchingLabel,Path=ActualWidth}" Height="{Binding ElementName=stretchingLabel,Path=ActualHeight}" HorizontalAlignment="Stretch"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="0.24*" /&gt; &lt;RowDefinition Height="0.62*" /&gt; &lt;RowDefinition Height="0.24*" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;my:CategoryLine VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="1" DataContext="{Binding ElementName=ShelfGrid, Path=DataContext}"&gt; &lt;/my:CategoryLine&gt; &lt;/Grid&gt; &lt;/TextBlock&gt; &lt;TextBlock Name="stretchedLabel" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="0" Padding="0"&gt; &lt;ItemsControl ItemsSource="{Binding Products}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;UniformGrid DataContext="{Binding}" Width="{Binding ElementName=stretchedLabel, Path=ActualWidth}" MaxHeight="{Binding ElementName=stretchedLabel, Path=ActualHeight}" Height="{Binding ElementName=stretchedLabel, Path=ActualHeight}" Columns="{Binding Path=DataContext.Slots, RelativeSource={RelativeSource AncestorType={x:Type Grid}}}" Rows="1" VerticalAlignment="Stretch" Name="ParentUniformGrid" HorizontalAlignment="Stretch" /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;ItemsControl&gt; &lt;Grid Height="{Binding ElementName=stretchedLabel, Path=ActualHeight}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="*" /&gt; &lt;RowDefinition Height="{Binding ElementName=ParentUniformGrid,Path=DataContext.SplitterSize}" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Image VerticalAlignment="Bottom" Grid.Row="0" HorizontalAlignment="Center" x:Name="ProductImage" Source="{Binding Converter={Utilities:ProductImageConverter}}" Height="{Binding ImageHeight}" MaxHeight="{Binding ImageHeight}" MouseLeftButtonUp="BuyProductUsingImage" /&gt; &lt;Label Padding="0" Margin="0 1 0 0" Grid.Row="1" Background="LightGray" VerticalAlignment="Stretch" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" HorizontalAlignment="Stretch"&gt; &lt;Label VerticalAlignment="Center" Margin="1" HorizontalAlignment="Center" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Background="#fff" Padding="0" MaxWidth="{Binding ElementName=ShelvesControl, Path=DataContext.PriceLabelWidthSize}" Width="{Binding ElementName=ShelvesControl, Path=DataContext.PriceLabelWidthSize}"&gt; &lt;Viewbox VerticalAlignment="Center" HorizontalAlignment="Center"&gt; &lt;StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" Margin="1" Background="#fff" HorizontalAlignment="Left"&gt; &lt;Label Background="Yellow" Padding="2 0 2 0" VerticalContentAlignment="Bottom" FontWeight="Bold" Content="{Binding Price}"&gt; &lt;/Label&gt; &lt;/StackPanel&gt; &lt;/Viewbox&gt; &lt;/Label&gt; &lt;/Label&gt; &lt;/Grid&gt; &lt;/ItemsControl&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;/TextBlock&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload