Question

I am absolutely new to WPF. I can't figure out why isn't the VirtualizingStackPanel working for the following. I already have experimented with the ScrollViewer but it doesnt' work for me. I have tried placing VirtualizingStackPanel every where but it seems to have no affect at all. The combobox when populated with 3000 item and each item has 16x16 image, takes around 30 seconds to show up. So any help will be appreciated.

Update: The datatemplate has been added

<DataTemplate x:Key="ComboBoxItemTemplate">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="20"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col1"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col2"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col3"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col4"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col5"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Image
            Width="16"
            Height="16"
            Margin="1"
            Source="{Binding Image}"/>
            <StackPanel Grid.Column="1" Margin="1">
                <TextBlock Text="{Binding DisplayName}"/>
            </StackPanel>
            <StackPanel Grid.Column="2" Margin="5,1,1,1">
                <TextBlock Text="{Binding Column2}"/>
            </StackPanel>
            <StackPanel Grid.Column="3" Margin="5,1,1,1">
                <TextBlock Text="{Binding Column3}"/>
            </StackPanel>
            <StackPanel Grid.Column="4" Margin="5,1,1,1">
                <TextBlock Text="{Binding Column4}"/>
            </StackPanel>
            <StackPanel Grid.Column="5" Margin="5,1,1,1">
                <TextBlock Text="{Binding Column5}"/>
            </StackPanel>
            <StackPanel Grid.Column="6" Margin="5,1,1,1">
                <TextBlock Text="{Binding InheritanceDescription}"/>
            </StackPanel>
        </Grid>
    </DataTemplate>

    <Style TargetType="{x:Type local:ComboBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ComboBox}">
                    <Grid x:Name="LayoutRoot" Width="Auto" Height="Auto">
                        <Grid.Resources>
                            <Style x:Key="AlternatingRows" TargetType="{x:Type ComboBoxItem}">
                                <Setter Property="Width" Value="{Binding Path=Dropdownwidth, RelativeSource={RelativeSource AncestorType={x:Type local:ComboBox}}}" />
                                <Setter Property="Background" Value="#FFE7ECF5"/>
                                <Setter Property="UIElement.IsEnabled" Value="{Binding IsEnabled}"/>
                            </Style>
                        </Grid.Resources>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Label
                            Grid.Column="0"
                            HorizontalAlignment="Left" HorizontalContentAlignment="Left"
                            Margin="4,2,4,2"
                            Visibility="{Binding Path=NameVisibility, RelativeSource={RelativeSource TemplatedParent}}"
                            Target="{Binding ElementName=combobox1}">
                            <AccessText Text="{Binding Path=DisplayName, Converter={StaticResource appendColon}, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Label>
                        <ComboBox
                         x:Name="combobox1"
                         IsEditable="False"
                         AlternationCount="2"
                         Width="{Binding Path=ChildWidth, RelativeSource={RelativeSource TemplatedParent}}"
                         Grid.Column="1"
                         Grid.IsSharedSizeScope="True"
                         HorizontalAlignment="Left"
                         ItemContainerStyle="{StaticResource AlternatingRows}"
                         Margin="0,2,4,2"
                         ItemTemplate="{StaticResource ComboBoxItemTemplate}"
                         SelectedItem="{Binding Path=SelectedItem, RelativeSource={RelativeSource TemplatedParent}}"
                         ToolTip="{Binding Path=Description, RelativeSource={RelativeSource TemplatedParent}}"
                         BorderBrush="#95B7F3">
                            <ComboBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <VirtualizingStackPanel VirtualizingStackPanel.IsVirtualizing="True"  VirtualizingStackPanel.VirtualizationMode="Recycling"/>
                                </ItemsPanelTemplate>
                            </ComboBox.ItemsPanel>
                        </ComboBox>
                        <StackPanel Grid.Column="2" Orientation="Horizontal" Width="Auto" Height="Auto">
                        <Label HorizontalAlignment="Left" HorizontalContentAlignment="Left" Margin="4,2,4,2" Height="Auto" Width="Auto"
                                Visibility="{Binding Path=UnitVisibility, RelativeSource={RelativeSource TemplatedParent}}"
                               >
                            <TextBlock Text="{Binding Path=DisplayUnit, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Label>
                        <Label Target="{Binding ElementName=combobox1}" HorizontalAlignment="Left" HorizontalContentAlignment="Left" Margin="4,2,4,2" Height="Auto" Width="Auto" MaxWidth="450"
                                Visibility="{Binding Path=DescriptionVisibility, RelativeSource={RelativeSource TemplatedParent}}"
                               >
                            <AccessText Text="{Binding Path=Description, RelativeSource={RelativeSource TemplatedParent}}" ToolTip="{Binding Path=Description, RelativeSource={RelativeSource TemplatedParent}}" TextTrimming="WordEllipsis" TextWrapping="Wrap"/>
                        </Label>
                        </StackPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
Was it helpful?

Solution

The property for your image - have you made it so it creates the image when you access the property and not in the constructor? It sounds like it is loaded in the constructor :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top