VirtualizingStackPanel不适用于ComboBox

时间:2011-03-23 19:48:51

标签: wpf wpf-controls controltemplate ui-virtualization

我是WPF的新手。我无法弄清楚为什么VirtualizingStackPanel不适用于以下内容。我已经尝试过ScrollViewer,但它并不适合我。我尝试过将VirtualizingStackPanel放在每个地方,但似乎根本没有任何影响。当使用3000个项目填充组合框并且每个项目具有16x16图像时,需要大约30秒才能显示。所以任何帮助都将不胜感激。

更新:已添加datatemplate

<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>

1 个答案:

答案 0 :(得分:1)

图像的属性 - 您是否已经创建了图像,以便在访问属性而不是在构造函数中时创建图像?听起来它是在构造函数中加载的:)